Python script essays - File operations

At first internship, you need to write a script with python, record what common file operations. File operations need to use the library: os

  • Path operations

 

    1. Get current path: os.getcwd (), the return value is a string. For example: rootdir = os.getcwd ()
    2. Path splicing: the os.path.join ( Parameter . 1, Parameter 2, Parameter . 3 .....), the return value according to the order parameter as a stitching path. E.g:
      1. Parent directory of the current directory: the os.path.join RootDir = (The os.getcwd (), ".." )
      2. Current path1 directory under: RootDir = os.path.join (os.getcwd (), "path1" )
    3. Determining whether the file exists: if os.path.isfile (path): path represented by the file path. For example: if os.path.isfile (rootdir + "file.txt"):
    4. Judgment folder exists: if os.path.exists (path): path represents a file folder. For example: if os.path.isfile (rootdir):
    5. Path Example:
      Import OS
       # Get the current path 
      Print  ' *** *** PathNow ' 
      Print The os.getcwd ()
       Print os.path.abspath with (os.path.dirname ( __FILE__ ))
       # obtain higher path 
      Print  ' *** ** HighPath * ' 
      Print os.path.abspath with (os.path.dirname (os.path.dirname ( __FILE__ )))
       Print os.path.abspath with (os.path.dirname (The os.getcwd ()))
       Print the os.path. ABSPATH (the os.path.join (the os.getcwd (), " .. " ))
       # obtaining higher path 
      Print  ' *** *** HigherPath ' 
      Print os.path.abspath(os.path.join(os.getcwd(), "../.."))

      Py sequentially output file is located and the higher the absolute path absolute path

  • File traversal:
    1. list = os.listdir(path1) 
          for i in range(0,len(list)):
              path = os.path.join(rootdir,list[i])
      

      list is a list of files in the path1, path is the absolute path to the file specified search files in path1: 

  • Find files
    1. The first search method: split a string
      if os.path.splitext(path)[1]==".txt"
      

      ".Txt" suffix file absolute path, path represents the absolute file path, file name for the last layer

    2. Function implemented for example
      DEF delate_type_file (target, filetype): 
          RootDir = os.path.join (os.getcwd (), target) 
          List = os.listdir (RootDir) # listed in the following folder all files and directories 
          for i in the Range (0, len (List)): 
              path = the os.path.join (RootDir, List [I])
               IF os.path.splitext (path) [. 1] == " .txt " : # .txt file extension, with the replacement filetype 
                  Print (os.path) 
                  os.remove (path)

      filetype for the file name suffixes

    3. The second search method: As of character determine the absolute file path
      if f.endswith(".txt")
      

      ".Txt" represents the absolute file path suffix, path represents the absolute file path, file name for the last layer

    4. Implementing a function Example:
      DEF delate_type_file2 (target, filetype): 
          RootDir = os.path.join (os.getcwd (), target) 
          List = os.listdir (RootDir) # List Folder all the files and directories 
          for f in List:
               IF f .endswith ( " .txt " )   # ".txt" file extension, can replace the filetype 
              # file operation

      filetype for the file name suffixes

  • Delete files
    1. First determine whether you want to delete the absolute path to a valid file, then delete
      if os.path.isfile(path):
          os.remove(path)
      

      os.remove function parameter is a string, you delete a file in which the absolute path

    2. Delete function Example:
      # Delete files: delete all the files in the current directory target folder 
      DEF delate_file (target): 
          RootDir = os.path.join (os.getcwd (), target) 
          Print RootDir 
          List = os.listdir (RootDir) # column a folder for all the files and directories 
          for I in Range (0, len (List)): 
              path = the os.path.join (RootDir, List [I]) 
              IF the os.path.isfile (path): 
                  Print (OS .path) 
                  os.remove (path)
      

      py target for the current file is located in a sub-folder file name

  • Rename the file
    1. Call the function: os.rename, you need to pass two parameters. The first parameter is the absolute path to the original file, the second parameter is the absolute path to the new file
      #path path to the file suffix old_name modify the file suffix new_name 
      DEF rename_file (path, old_name, new_name): 
          List the os.listdir = (path) 
          for F in List: 
              Portion = os.path.splitext (F) 
              Portion IF [. 1] == OLD_NAME: 
                  new new = Portion [0] + NEW_NAME 
                  filenamedir + = path "\\" + F 
                  newnamedir + = path "\\" + new new 
                  Print (filenamedir) 
                  Print (newnamedir) 
                  os.rename (filenamedir , newnamedir)
      

      filenamedir absolute path to the original file, newnamedir absolute path for the new file

  • Copy files
    1. Shutil copyfile need to call the library functions, copyfile function takes two arguments. The first parameter is the absolute path to the original file, the second parameter is the absolute path to the new file
      # Copy file: 1 copy from the path of the file to the path specified type 2, need to call shutil copyfile library functions 
      from shutil Import copyfile 
      DEF copy_file (path1, path2, last_name): 
          List = os.listdir (path1) # List files all the clip directories and files 
          for I in Range (0, len (List)): 
              path = the os.path.join (RootDir, List [I]) 
              IF path.endwith (last_name): 
                  CopyFile (path1, path2)

 

Guess you like

Origin www.cnblogs.com/Noble-Monster/p/11230522.html