Python 用file 对象和open方法处理文件的区别
来源:互联网 发布:淘宝店铺如何改名字 编辑:程序博客网 时间:2024/06/04 19:16
最近用python处理文件时碰到一个错误,让我仔细对这种用法有了一些深入的研究:
python document 是这么说的:
File objects are implemented using C’s stdio package and can be created with the built-inopen() function. File objects are also returned by some other built-in functions and methods, such asos.popen() andos.fdopen() and themakefile() method of socket objects. Temporary files can be created using thetempfile module, and high-level file operations such as copying, moving, and deleting files and directories can be achieved with theshutil module.
并且对File 对象的构造函数说明如下:
file(filename[,mode[, bufsize]])
Constructor function for the file type, described further in sectionFile Objects. The constructor’s arguments are the same as those of theopen() built-in function described below.
When opening a file, it’s preferable to use open() instead of invoking this constructor directly.file is more suited to type testing (for example, writingisinstance(f, file)).
New in version 2.2.
- 但是其实我在如下代码段中,
- def setNodeManagerDomain(domainDir):
try:
domainName = os.path.basename(domainDir)
fd = open(domainDir + '/nodemanager/nodemanager.domains', 'w')
fd.write('#Domains and directories created by Configuration Wizard.\n')
fd.write('#' + time.ctime() + '\n')
dirNorm=os.path.normpath(domainDir).replace('\\','\\\\')
fd.write(domainName + '=' + dirNorm)
print 'create domain file and close in the end under the directory:' + domainDir
fd.close
except Exception, e:
print 'Failed to create domain file in the directory:' + domainDir - 我使用file对象 or open方法在windows 环境下都能通过,但是程序部署到Linux环境中就出现问题。
[echo] NameError: file
- 可能linux环境对file支持不好,所以保险起见,还是遵循文档中所说的,坚持用open方法吧。
- Python 用file 对象和open方法处理文件的区别
- Python 文件处理 file 与 open 的区别与注意
- Python的file()和open()方法的区别
- Python中file()和open()的区别
- Python文件处理 open()
- Python的File(文件) 方法
- python的文件处理函数open
- Python 读写文件和file对象
- Python:file/file-like对象方法详解【单个文件读写】
- 【python】file.write() 和 open()
- Python 之 open() 和file()
- python:open方法的文件操作
- 对python文件方法open的探究
- 对python文件方法open的探究
- Python 文件I/O和File方法
- window对象的open和close方法
- Python open() 函数 文件处理
- 有关file文件处理的常用方法
- 解析Domain
- Sql Server 手工注入总结(显错模式)
- 新年到来两天,把mxgraph1.9.0.2给破解了.爽一把先...
- SQL注入--入侵动网
- 鼠标向上向下滚 转载自p2227
- Python 用file 对象和open方法处理文件的区别
- Linux下让Oracle服务自动启动与停止
- 泛型结合多态
- 菜鸟+Sa+注入工具组合=肉鸡成群
- 屏蔽优酷、土豆等视频网站30秒广告的最全最简单方法
- ' or '1'='1'等漏洞问题
- java张孝祥(6) 交通灯管理系统
- 快速学习,理解.SQL注入技术
- JS document.selection.createRange方法