File operations

Read and write operations on files

# file read; R is read mode; FileNotFoundError file_name = open("name.txt", "r", Encoding =" utF-8 ") print(file_name.readlines()) file_name.close(); W indicates the write mode. If no file exists, the file is created. File_name = open("name2.txt", "w", encoding=" utF-8 ") file_name.write("Python") file_name.close() # Append to file; A indicates the append mode. If the file does not exist, the file is created. File_name = open("name2.txt", "a", encoding=" utF-8 ") file_name.write("Python") file_name.close() # Open file in binary mode; B is the binary open file. Used with write or read mode; File_name = open("name.txt", "rb") file_name_copy = open("name_copy.txt", "Wb ") file_name_copy.write(file_name.read()) file_name_copy.close() file_name_copy.close() # open with read/write; + open for read/write mode; File_name = open("name2.txt", "r+", encoding="UTF-8") # To call both read and write methods, Print (file_name.readlines()) file_name.write("Python_r+") file_name.close()Copy the code

Common functions for file operations

# file_name = open("name.txt", "r", encoding=" utF-8 ") # print(file_name.read()) Print (file_name.readline()) # print(file_name.readlines()) # print(file_name.readlines()) # print(file_name.readlines()) # print(file_name.readlines()) # print(file_name.readlines()) # print(file_name.readlines()) # file_name = open("name.txt", "w", Encoding =" utF-8 ") # file_name.write("Python") # file_name.writeline (["Python", # file_name.close() file_name = open("name.txt", "r", Print (file_name.tell()) # 2; print(file_name.tell()) # 2; Print (file_name.read()) # print(file_name.read()) Print (file_name.tell()) # print(file_name.seek(0)) # print(file_name.seek(0)) # print(file_name.tell()) # print(file_name.seek(0)) Print (file_name.tell()) print(file_name.readline()) print(file_name.tell()) print(file_name.readline() File_name.seek (0) print(file_name.readlines()) file_name.flush() File_name.close () # close the fileCopy the code

Use of the with statement

# print(type(open("name.txt", "r", encoding=" utF-8 ")) # <class '_io.TextIOWrapper'>; TextIOWrapper instance object is a context manager # with open("name.txt", "r", encoding="UTF-8") as file_name: # print(file_name.read()) # print(file_name.read()) # print(file_name.read()) # print(file_name.read()) # print(file_name.read()) # print(file_name.read()) Class MyContextManager(Object): def __enter__(self): print("MyContextManager __enter__ called") return self def __exit__(self, exc_type, exc_val, exc_tb): print("MyContextManager __exit__ called") def show(self): print("MyContextManager show called") with MyContextManager() as context_mgr: Context_mgr. show() """ log message:  MyContextManager __enter__ called MyContextManager show called MyContextManager __exit__ called """Copy the code

The use of OS modules

Import OS # # open notepad # os.system("notepad.exe") # # Open calculator # os.system("calc.exe") # # Call executable # Os.startfile (r"D: ToolList\ vscode-win32-x64-1.44.2 \ code.exe ") # print(os.getcwd()) # return the files and directories in the specified path print(os.listdir(".. /chap14")) # print(os.listdir(os.getcwd())) # print(OS # os.removedirs("mkdir/dir") # # Set path to the current working directory. /chap13") # print(os.getcwd())Copy the code

The use of the os.path module

Print (os.path.abspath("demo5.py")) print(os.path.exists("demo5.py")) Print (os.path.exists("demo50.py")) print(os.path.join(r"E:\PyCharmProjects", Print (os.path.split(os.path.abspath("demo5.py")) Print (os.path.basename(os.path.abspath("demo5.py"))) print(os.path.basename(os.path.abspath("demo5.py")) Print (os.path.dirname(os.path.abspath("demo5.py") print(os.path.isdir(os.path.basename(os.path.abspath("demo5.py")))) Isdir (os.path.dirname(os.path.abspath("demo5.py")))) Including child files in directories and subdirectories print (" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ") file_list = OS. Walk (OS) getcwd ()) for dirpath, dirname, filename in file_list: for dir in dirname: print(os.path.join(dirpath, dir)) for file in filename: print(os.path.join(dirpath, file)) print("*************************************")Copy the code