python tool - move files and folders and treatment

 A large number of files and folders to sort through the python: restructuring, move, delete and so on.

 Commonly used functions are summarized as follows:

 1. os.path.dirname (__ file__): Gets the absolute path of the folder where the script py;

 2. os.path.abspath (file_path): Gets the absolute path of the file, including the file name

 3. os.path.join (path, filename): The path and file folder path stitching together, attention must be in the path where the file folder, other folders no intervening therebetween;

 4 .os.path.splitext (filename): cutting the file name for the name and the suffix;

   5. os.path.isdir (file): determines whether the folder;

 6. os.path.isfile (file): determines whether the file;

 

  File traversal:

      for root ,dirs, files in os.walk(path):

  .....

  It is an iterator, each iteration goes through a hierarchical file structure, that is, after each iteration through all the documents and files in the current folder level, then continue to traverse folders following files and folders. To all of 7 as a target file operations such as copy, we should get the path of the file, then this can be spliced ​​directly to give: os.path.join (root, file). It is noteworthy that if the latter document processing depends on a number of previous treatment, then be careful, you can not assume that the previous process will occur during the first iteration, then relatively simple and crude but reliable way is a re the new wheel traversal iteration preceding process depends so must have occurred.

 

Guess you like

Origin www.cnblogs.com/gadflyWZQ/p/11074866.html