python basis (14): File and folder operations

File Operations

open function:

         In python, using the open function to open an existing file or create a new file.

open(name[, mode[, buffering[, encoding]]])
  •  name: a string value containing the name of the file you want to access (to distinguish between absolute and relative paths).
  • mode: mode determines the open file modes: read-only, write, append and so on. See a complete list of all possible values ​​as follows. This parameter is not mandatory, the default file is read-only access mode (r).
  • buffering: If the value of buffering is set to 0, there will be storage. If the value of buffering take 1, the line will register to access the file. If the value is an integer greater than 1 of buffering, indicating this is the buffer size storage area. If negative, the buffer size for the system default parking zone.

Access mode:

  • r: read-only open the file. Pointer file will be placed at the beginning of the file.
  • w: Write only way to open the file. If the file already exists it will be overwritten. If the file does not exist, create a new file.
  • a: open a file for additional content, if there is open, there is no new file is created.
  • r +: read and write, the file pointer transferred speaks header file.
  • w +: read and write, the file does not exist directly created, covering the source file exists.
  • a +: additional write, the file will be transferred to the end of the file pointer.

 

 File reading and writing:

        read (num): you can read the contents of a file inside. num is the length of the file from which data is read (in bytes), if no incoming num, will read all of the data file.

# 打开文件
files = open('python.txt', 'r', encoding = 'utf-8')
# 读入内存
content = files.read()
# 输出
print(content)
# 关闭文件
files.close()

with...open...

        Keywords with no need to access the file after it is closed. This allows python to determine: You just open the file and use it when needed, since the python will automatically turn it off at the right time.

        You can also call open () and close () to open and close the file, but doing so, if there is a program bug, leading to close () statement is not executed, the file will not be closed.

with open('python.txt', 'r', encoding = 'utf-8') as files:
    content = files.read()
    print(content)

readlines():

        The contents of the entire file can be read in one time line manner, and returns a list, where each data row as an element.

files = open('python.txt', 'r', encoding = 'utf-8')
content = files.readlines()
print(content)
files.close()

Write to the file:

  • write () no output terminal
  • write () does not add line breaks at the end of the text is written, you need to manually add \ n.
with open(filename, 'w') as file_object:
    file_object.write("I love programming.")

 

Folder actions:

        os模块包含普遍的操作系统功能,与具体的平台无关。

import os

获取当前路径:

print(os.getcwd())

 列出当前或指定目录下的文件和文件夹:

pring(os.listdir())

判断是否是一个文件:

print(os.path.isfile('.\\demo.txt'))

判断是否存在:

print(os.path.exists('.\\demos.txt'))

重命名文件:

os.rename('demo.txt', 'demo1.txt')
# 把demo改成demo1

删除文件:

os.remove('demo1.txt')

把路径和文件分开:

print(os.path.split('F:\python\demo.py'))
# output:('F:\python', 'demo.py')

创建文件夹:

os.mkdir('study') # 创建了名叫study的文件夹

删除文件夹:

os.rmdir('study') # 删除名叫study的文件夹

 

Guess you like

Origin blog.csdn.net/qq_26271435/article/details/90107968