Home  >  Article  >  Backend Development  >  Python knowledge of file operations

Python knowledge of file operations

高洛峰
高洛峰Original
2017-03-02 16:44:271110browse

This article mainly introduces relevant information about Python's summary of file operation knowledge. It is of great reference value. Friends in need can refer to

Opening files

Operation files

1When opening a file, you need to specify the file path and opening method

Opening method:

r: Read-only
w: Write-only
a: Append

"+" to indicate that a file can be read and written simultaneously

r+: Read and write
w+: Write and read
a+: Same as a

U " means that \r \n \r\n can be automatically converted to \n when reading (used with r or r+ mode)

rU
r+U

"b" means processing binary files (such as: FTP sending and uploading ISO image files, Linux can be ignored, Windows needs to be marked when processing binary files)

rb
wb
ab

f = open('test.log','r+',encoding='utf-')
f.write('saf中sdhgrbfds')
print(f.tell()) #查看当前指针位置,以字符为单位
f.seek() #指定当前指针位置,以字节为单位
print(f.read())
f.truncate() #读取指针之前的数据
print(f.tell())
f.close()

2: Common file operations

f = open('data', 'r') # Open in read-only form (the default is read-only)
f = open('f.txt', encoding='latin-1') #python3.0 Unicode file
string = f.read() #Put The file is read into a string
string = f.read(N) #Read N bytes after the pointer
string = f.readline() #Read the next line, including the end-of-line identifier
alist = f.readlines() #Read the entire file into a list of strings
f.write() #Write strings to the file
f.writelines() #Write all strings in the list to the file
f.close() #Manually close
f.flush() #Flash the output buffer to the hard disk
f.seek(N) #Move the file pointer to N, in bytes Unit
for line in open('data'):
print(line) #The file iterator reads the file line by line
open('f.txt','r').read() #read all at ance into string

Three: Store and parse python objects in files

x,y,z = 41,42,43
s = 'spam'
D = {'a':1, 'b':2} #字典对象
L = ['a','b','c'] #列表
f = open('f.txt','w')
f.write(s + '\n')
f.write('%s,%s,%s\n'%(x,y,z))
f.write(str(D))
f.write('\n')
f.write(str(L))
f.close()
print(open('f.txt').read()) #将文件内容输出
#从文件中取出数据,并判断其类型
'''
a = fi.readline()
b = fi.readline()
c = fi.readline()
d = fi.readline()

print(a,b,c,d,type(a),type(b),type(c),type(d))
'''
# 从文件中取出数据,并转换为存储前的类型
fi = open('f.txt')
a = fi.readline().rstrip() #rstrip()去掉换行符
print(a,type(a))
b = fi.readline().rstrip().split(',') #字符串的split()方法,在括号中写入分隔符,将字符串分割为列表。
print(b,type(b))
c = fi.readline()
C = eval(c) #调用内置函数eval(),将字符串转化为可执行的python代码。
print(C,type(C),type(c))
d = fi.readline()
D = eval(d)
print(D,type(D),type(d))

The above is the summary of Python's file operation knowledge introduced by the editor. I hope it will be helpful to everyone!

Please pay attention to PHP Chinese for more articles related to Python's file operation knowledge. net!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn