This paper analyzes the methods of Python file and directory operation. Share to everyone for your reference, as follows:
For a detailed description of the Python file operation, you can refer to the previous article "Python file operations related knowledge points summary and collation"
Official Api:os-miscellaneous Operating system interfaces
Here is a demo example:
Import reimport osimport time# picture file path image_path = ' e:\\test\\20130627_140132hongten.jpg ' #文件夹路径dir_path = ' e:\\test\\ Hongten ' #文件路径file_abs_path = ' e:\\test\\hongten.txt ' #得到当前工作空间目录def getcwd (): Return os.getcwd () #获取指定文件夹下面的所有文件及文件夹 # If the specified folder does not exist, the appropriate message is returned DEF listdir (Dir_path): If Os.path.exists (Dir_path): Return Os.listdir (Dir_path) Else:return ' Contents ' + Dir_path + ' does not exist ' Def isfile (file_path): If Os.path.exists (file_path): Return Os.path.isfile (file_path) Else: Return ' file ' + Dir_path + ' does not exist ' if __name__ = = ' __main__ ': print (' current workspace is: {0} '. Format (GETCWD ())) print (' files and directories under current workspace: '), Listdir (GETCWD ()) Print (' # ' *) print (Listdir (' C:\\test ') print (' # ' *) print (Isfile (image_path)) print (' # ' * 4 0) array = os.path.split (image_path) print (array) #文件全名: 20130627_140132hongten.jpg file_full_name = array[1] name = O S.path.splitext (file_full_name) #文件名: 20130627_140132hongten file_name = name[0] #文件后缀:. jpg file_ext = name[1] Print (' Full file name: {0}, FileName: {1}, file suffix: {2} '. Format (filE_full_name,file_name,file_ext) print (' # ') #创建空文件夹 #os. mkdir (' E:\\mydir ') #创建多级目录 #os. Makedirs (R ' e:\\bb\\cc ') Print (' # ') #打开一个文件 fp = open (File_abs_path, ' w+ ') #print (' Read the file: The first line of {0}: {1} '. Format (File_abs_path,fp.readline ()) # Take each line of the file as a member of a list and return to the list. In fact, its internal is through the Loop call ReadLine () to achieve. #如果提供size参数, size is the total length of the read content, which means that it may be read only to a portion of the file. #print (' Read file: {0} All contents: {1} '. Format (File_abs_path,fp.readlines ())) content = ' This is a test message!! \ngood boy!\ngogo......\nhello,i\ ' m hongten\nwelcome to my space! ' Fp.write (content) Fp.flush () fp.close () fp = open (File_abs_path, ' r+ ') print (' Read file: {0} All contents: {1} '. Format (File_abs_path, Fp.readlines ()))
Operating effect:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> Current Workspace is: D:\Python33\workspace files and directories under current workspace: [' rename.py ', ' test_annotation.py ', ' test_class.py ', ' test_exception.py ', ' test_exit.py ', ' test_file.py ', ' test_geta.py ', ' test_hello.py ', ' test_import.py ', ' test_ input.py ', ' test_loops.py ', ' test_myclass.py ', ' test_os.py ', ' test_range.py ', ' test_str.py ', ' test_string.py ', ' test_ while.py ', ' test_with.py ']####################################### #目录c: \test does not exist ################################# ###### #True ######################################## (' e:\\test ', ' 20130627_140132hongten.jpg ') file Full name: 20130627_ 140132hongten.jpg, FileName: 20130627_140132hongten, file suffix:. jpg######################################################## ####################### #读取文件: E:\test\hongten.txt All content: [' This is a test message!! \ n ', ' good boy!\n ', ' gogo......\n ', ' hello,i ' m hongten\n ', ' Welcome to my space! '] >>>
I hope this article is helpful for Python program design.