file object = open(file_name [, access_mode][, buffering])
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开一个文件
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
Name of the file: foo.txt Closed or not : False Opening mode : wb Softspace flag : 0
fileObject.close();
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开一个文件
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
# 关闭打开的文件
fo.close()
Name of the file: foo.txt
fileObject.write(string);
例子:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开一个文件
fo = open("/tmp/foo.txt", "wb")
fo.write( "Python is a great language.nYeah its great!!n");
# 关闭打开的文件
fo.close()
Python is a great language. Yeah its great!!
fileObject.read([count]);
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开一个文件
fo = open("/tmp/foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# 关闭打开的文件
fo.close()
Read String is : Python is
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开一个文件
fo = open("/tmp/foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# 查找当前位置
position = fo.tell();
print "Current file position : ", position
# 把指针再次重新定位到文件开头
position = fo.seek(0, 0);
str = fo.read(10);
print "Again read String is : ", str
# 关闭打开的文件
fo.close()
Read String is : Python is Current file position : 10 Again read String is : Python is
os.rename(current_file_name, new_file_name)
#!/usr/bin/python # -*- coding: UTF-8 -*- import os # 重命名文件test1.txt到test2.txt。 os.rename( "test1.txt", "test2.txt" )
os.remove(file_name)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
# 删除一个已经存在的文件test2.txt
os.remove("text2.txt")
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有