In this paper,

and ( Click to open link ) is a sister article, which mainly describes the reading operation of Python for file information.

Python reads the file information in its inherent class OS.

to read all the following folder under the F disk as an example to illustrate the problem:

Python" there are two way for folder traversal, a direct walk method directly using the packaged. This is
Python do better than Java advantage:

 -*-coding:utf-8-*- import OS for # root, dirs, files in os.walk ("f:\"): for dir in dirs: print os.path.join (root, DIR).Decode ('UTF-8').Encode ('utf-8'); for file in files: print os.path.join (root, file).Decode ('UTF-8').Encode ('utf-8'); 


following effect running


of the program, os.walk will read all the path root, directory name dirs and files file name, which is the three array using foreach cycle output file. The

join method is to connect the path to the directory name or the file name to make up a complete directory.

('UTF-8').Encode back.Decode ('utf-8'); is the main method to Eclipse under the control of PyDev use, if the output without the console will be garbled, but if your Python program is to pull into the Windows console CMD operation using Python, please don't add this encoding method, and removed at the beginning of the # -*-coding:utf-8-*-. Because CMD is encoded in UTF-8 simplified Chinese, not Linux - like terminals are UTF-8 coding.

can also be based on the recursive idea of Java, written in the following form:

 -*-coding:utf-8-*- import OS #; files = list (DEF); DirAll (pathName): if os.path.exists (pathName): fileList = os.listdir (pathName); for F in fileList: if f== "$RECYCLE.BIN" or f== "System Volume Information" continue; f=os.path.join (pathName, f); if os.path.isdir (f): DirAll (f); else: dirName=os.path.dirname (f); baseName=os.path.basename (f); if dirName.endswith (os.sep): files.append (dirName+baseName); else: files.append (dirName+os.sep+baseName); DirAll ("f:\"); for F in files: print f.decode ('UTF-8'.Encode) ('utf-8'); 


following effect running

of course, this form has its drawbacks, is not some folder traversal system, such as $RECYCLE.BIN, System Volume Information, if you do not write the judgment conditions, will lead to the read error. Therefore, when traversing folders later, it is also the way to recommend the os.walk on the top of the time.

, however, we can see several common usage from the above procedures, os.sep can directly return the file separator, and / or os.path.join ( path, file directory path and file name) to the file directory name connects the os.path.isdir (full path) can determine whether the directory, endswith ("XX") method to determine whether the string to the end of the xx. Os.path.dirname () can get the path of the directory where the file is located. Os.path.basename () can get the file name, os.listdir (path) can get the corresponding file list, and so on.

is the whole content of this article, I hope to help you, and hope that you can support a lot of scripting home.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Python traverses all folders and file paths under a directory | Script Home

You may also be interested in these articles!