文件2019-03-20

2019-03-20  本文已影响0人  swagsmile
#open()方法的完整语法格式
open(file,mode ='r',buffering = -1,encoding = None,errors = None,newline = None,closefd = True,opener = None)
#常用的open()方法
open(file, mode='r') #两个参数:文件名(file)和模式(mode)

注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法。

with open(file_name) as somefile:
    do something(somefile) 
close()
fileObject.close()
read()
fileObject.read([count])#count为从已经打开文件中读取的字节计数。该方法从文件的开头开始读入,如果没有传入count,它尽可能多的读取内容,很可能是直到文件的末尾。
# os模块可以重命名和删除文件
os.rename(current_file_name, new_file_name)
# os模块可以帮你创建,删除和更改目录。
os.mkdir("newdir")
os.chdir("newdir")
os.getcwd() #显示当前的工作目录
上一篇 下一篇

猜你喜欢

热点阅读