"" " Rename files os.rename (src, dst) os.rename ( '123.txt', '124.txt') to delete the file os.remove (path) os.remove ( '123.txt') to create the directory os.mkdir () to create multi-level directory os.makedirs () delete the directory os.rmdir () delete levels of directories os.removedirs () to get the current directory os.getcwd () to modify the directory os.chdir () to determine whether a file exists os.path.exists () to determine whether the file os.path.isfile () to determine whether the directory os.path.isdir () Gets absolute path os.path.abspath () to determine whether an absolute path os.path.isabs ( ) Get the final portion of the path os.path.basename () Gets the parent path os.path.dirname() 获取文件夹内的子文件(重要) os.listdir() """ import os # os.rename("log.txt","log.properties") # os.remove("log.properties") # open("log.txt","w") # os.mkdir("abc") # os.rmdir("abc") # os.makedirs("a/b/c/d") # os.removedirs("a/b/c/d") # os.chdir("../") # print(os.getcwd()) # file_name = "abc.jpeg" # if os.path.exists(file_name): # if os.path.isdir(file_name): # print("删除文件夹成功") # os.rmdir(file_name) # elif os.path.isfile(file_name): # the else: # os.remove (file_name) # Print ( "File successfully deleted") # print("文件不存在") path = os.getcwd() for f in os.listdir(path): if f.endswith(".py"): print(f)
The default file encoding to open this file
with open("your_file", 'rb') as fp: file_data = fp.read() result = chardet.detect(file_data) file_content = file_data.decode(encoding=result['encoding'])
Several cases
1. Package # a custom function, Iterable all data objects may be written to the target file in the file, # Iterable if not stored in a string, the string conversion processing to DEF write_lines (file, Iterable): with Open ( File, MODE = "A", encoding = "UTF-. 8") AS F: for I in the Iterable: IF the isinstance (I, STR): f.write (I + '\ n-') the else: f.write (STR (I) + "\ n-") # write_lines ( "1.txt", [1,2,3,4,5,6, "ABCDE"])
# 2. a custom function, copy the file can be achieved (first read the target file, and then write the new file) # example: def copyfile (file) a copy of the file is copied out of a copy of a file named file_ suffix DEF CopyFile (File): filetup = file.rpartition ( ".") NEW_NAME = STR (filetup [0]) + "_ copy" + STR (filetup [. 1] + filetup [2]) IF the os.path. EXISTS (File): with Open (File, MODE = "R & lt", encoding = "UTF-. 8") AS F: with Open (NEW_NAME, MODE = "W", encoding = "UTF-. 8") AS F1: Content f.read = (1024) the while Content: f1.write (Content) Content = f.read (1024) the else: Print ( "File not EXISTS") # the CopyFile ( "digui.py")
# 3. Print a file folder of all the file names DEF print_all_file (file_path): file_list = os.listdir (file_path) for f in file_list: f = file_path + "\\" + f IF os.path.isfile ( f): Print (f) elif os.path.isdir (f): print_all_file (f) # print_all_file (r "E: \ Python Workspace \ Day13")
# 4. Try to have all before adding the .py file name in a folder on your name prefixed (note the file back up) # example: test01.py -> xxx_test01.py DEF add_prefix (file_path, prefix): file_list = the os.listdir (file_path) for file_list in F: F = file_path + "\\" + F IF The os.path.isfile (F): the basename = os.path.basename (F) dirname os.path.dirname = (F ) # Print (dirname + "\\" + prefix + basename) os.rename (f, dirname + "\\" + prefix + basename) elif os.path.isdir (f): add_prefix (f, prefix) # add_prefix ( r "E: \ python workspace \ day13", "songdan_")
# 5. encapsulates a function, you can achieve a similar operating system fuzzy queries (enter a keyword, you can display all the files in the target folder containing your keywords) Import the chardet DEF find_file_contains_key (f, Key): IF Key in f: print (f "contains a file named {key}: {F}:") with Open (F, MODE = "RB") AS FP: file_data = fp.read () Result = chardet.detect (file_data) FILE_CONTENT = file_data.decode (encoding = Result [ 'encoding']) now_fname = "" for in FILE_CONTENT Line: Line = line.strip () IF in Line Key: IF now_fname == "": now_fname = F.Name Print (F " {key}. comprising content file name: {f.name}: ") Print (F "\ T \ T content: {line}") show_file_by_word DEF (file_path, Key): file_list = os.listdir (file_path) for f in file_list: f = file_path + "\\" + f # file, which contains the keyword query if os.path.isfile (f) and f.endswith ( "Py."): find_file_contains_key (f, Key) # folder, find the files inside elif os.path.isdir (f): show_file_by_word (f, Key) show_file_by_word (r "E: \ Python Workspace \ day13 "," songdan ")
# 6. Statistical few lines of code in a folder all .py files in the COUNT = 0 DEF count_file_words (file_path): file_list = os.listdir (file_path) , Ltd. Free Join COUNT for f in file_list: f = file_path + "\\" F + IF The os.path.isfile (F) and f.endswith ( "Py."): with Open (F, MODE = "R & lt", encoding = "UTF-. 8") AS F: for F in Line: Line line.strip = () IF "#" and not in Line len (Line)> 0: COUNT + = 1 # folder, find the files inside elif os.path.isdir (f): count_file_words (f) return COUNT # count_file_words (r "E:\python workspace\day13\test") print(count)