uchr4 发表于 2015-12-15 08:56:32

file operate in python (open write read close )

1. file open:
    fd = open(N1,N2):N1:path of the file to be open like '/home/warrior/Downloads/test.txt'
                                 N2:somechoiceable parameter
                                        'r':read only   'w':write only   'r+':read and write    'a':write and will add content at the end of file
2. file read:
    content = fd.read():read all content of the file
    oneline = fd.readline():read one line content of the file
    lastContent = fd.readlines():read the content from current vernier to the end of the file
3. file write:
    fd.write(content):when opening in 'w' it will write content cover the original content
                              when opening in 'a' it will write content add at the end of the file but not start one new line again
    fd.writelines(content): when opening in 'w' it will write content cover the original content
                                        when opening in 'a' it will write content add at the end of the file and start one new line again
4. file close:
    fd.close():when open one file must close it as you will not use it .
5. file others:
    fd.seek(number):set the vernier of the file
页: [1]
查看完整版本: file operate in python (open write read close )