Common file and directory operations in python (1)

Common file operations

1. Open the file

  • open, it is a built-in function that can be called directly
  • Syntax: file object = open(file_name, [access_mode]) , here we will create a file object
  • Parameters: file_name--the string value of the file name to be accessed , access_mode--determines the mode of opening the file: read-only, write, append, etc. This parameter is optional, the default file access mode is read-only (r)
  • Return value: returns a file object
model explain
Open the file as read-only. The file pointer will be placed at the beginning of the file. This is the default mode
r+ Open a file for reading and writing. The file pointer will be placed at the beginning of the file
w Open a file for writing only. Overwrite the file if it already exists. Create a new file if it doesn't exist
w+ Open a file for reading and writing. Overwrite the file if it already exists. Create a new file if it doesn't exist
a Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, create a new file for writing
a+ Open a file for writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, create a new file for reading and writing

 

 

 

 

 

 

 

 

 

 

2. Close the file: file_object.close()

3. File read and write operations

read() method:

  • The read(size=-1) method reads a string from an open file
  • Parameters: size--pass the number of bytes you want to read, if not passed, the default is -1, read all the data,
  • After reading it. The pointer will be positioned to the position after reading
  • Return value: size=0, return b"", size<0, return all bytes read, size>0, return the specified number of bytes

readline() method:

  • Read entire line from file, including "\n" characters
  • Parameters: size--If a non-negative parameter is passed, returns the number of bytes of the specified size
  • Return value: Returns the bytes read from the string

readlines() method:

  • Read all lines and return a list of strings, or an empty string if EOF is encountered
  • Parameters: none
  • Return value: return a list of all strings

write() method:

  • The write() method writes any string to an open file
  • Note: The write() method does not add a newline character ("\n") at the end of the string, so automatic newline cannot be implemented. You need to manually add "\n" to achieve line breaks

writelines() method:

  • Pass in the content of the list you want to write , you can write multiple lines

4. Example

Suppose a new text file test.txt is created in the working directory of pycharm. Since it contains Chinese, we set the encoding to "utf-8" when saving. The contents of the file are as follows:

name: Peter
age: 29
height: 175
weight: 70
country: 英国
hobby: play-guitar

way of reading

#open()和read()方法

fo = open("test.txt")

content = fo.read()
print(content)

result:
唘縩ame: Peter
age: 29
height: 175
weight: 70
country: Baobian
hobby: play-guitar

Garbled, we can solve it like this:

fo = open("test.txt", encoding="utf-8")

content = fo.read()
print(content)

result:
name: Peter
age: 29
height: 175
weight: 70
country: United Kingdom
hobby: play-guitar

#readlines()方法
fo = open("test.txt", encoding="utf-8")

lines = fo.readlines()
print(lines)

['\ufeffname: Peter\n', 'age: 29\n', 'height: 175\n', 'weight: 70\n', 'country: 英国\n', 'hobby: play-guitar\n', '\n']

write method

write() method: if the file already exists, it will be overwritten

#write()方法

fo = open("test.txt", "w")

fo.write("city: London")

fo.close() 我们打开test.txt文件会发现,文件只剩下: city: London

注意:在同一个w模式下,两次写入时,第一次写入会已经存在的内容,第二次写入不会覆盖第一次写入的内容(可以理解为指针移了)

fo = open("test.txt", "w", encoding="utf-8")

fo.write("第一次写入会覆盖已经存在的内容,")
fo.write("第二次写入不会覆盖第一次写入的内容")
fo.close() 结果: 第一次写入会覆盖已经存在的内容,第二次写入不会覆盖第一次写入的内容

write()无法实现自动换行,需要手动添加"\n"来实现换行

fo = open("test.txt", "w", encoding="utf-8")

fo.write("第一次写入会覆盖已经存在的内容,\n")
fo.write("第二次写入不会覆盖第一次写入的内容")

fo.close()

结果:

第一次写入会覆盖已经存在的内容,
第二次写入不会覆盖第一次写入的内容

writelines()传递列表来写入多行,不会自动换行,需手动添加

fo = open("test.txt", "w", encoding="utf-8")

fo.write("第一次写入会覆盖已经存在的内容,\n")
fo.write("第二次写入不会覆盖第一次写入的内容\n")
fo.writelines(["111111\n", "222222\n"])

fo.close()

结果

第一次写入会覆盖已经存在的内容,
第二次写入不会覆盖第一次写入的内容
111111
222222

追加的方法

fo = open("test.txt", "a", encoding="utf-8")
fo.write("哈哈哈哈哈哈")

fo.close()

结果:

第一次写入会覆盖已经存在的内容,
第二次写入不会覆盖第一次写入的内容
111111
222222
哈哈哈哈哈哈

5. 文件定位

tell()和seek()方法详见:【译】:文件对象的方法

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325118331&siteId=291194637