python is to achieve all folder rename a file or folder

List the name of the file or files os.listdir () method returns the specified folder contains the folder.

It does not include . And .. even if it is in the folder.

Supports only be used under Unix, Windows.

os.path module is mainly used to acquire properties file.

The following are several common methods os.path module:
Method Description
os.path.abspath (path) returns the absolute path
os.path.basename (path) Returns the file name
os.path.commonprefix (list) returns the list (multiple paths), the total of all the longest path path
os.path.dirname (path) Returns the file path
os.path.exists (path) path if a path exists, returns True; path if the path does not exist, returns False.
Returns True os.path.lexists path exists, the path returns True damage
os.path.expanduser (path) contained in the path " - " and " - User " is converted into the user directory
os.path.expandvars (path) contained in the alternative path based on the value of the environment variable " $ name " and " $ {name} "
os.path.getatime (path) Returns the last access time (floating-point number of seconds)
os.path.getmtime (path) returns the most recent file modification time
os.path.getctime (path) Returns the file path creation time
os.path.getsize (path) Returns the file size, file does not exist if it returns an error
os.path.isabs (path) to determine whether the absolute path
os.path.isfile (path) determines whether a file path
os.path.isdir (path) determines whether the directory path
os.path.islink (path) determines whether the route link
os.path.ismount (path) determines whether the mount point path
os.path.join (path1 [, path2 [, ...]]) the synthesis of a directory and file name path
os.path.normcase (path) and the case of the conversion path slash
os.path.normpath (path) path string specification
os.path.realpath (path) returns true path path
os.path.relpath (path [, start]) calculated from the relative path start
os.path.samefile (path1, path2) determines whether the same file or directory
os.path.sameopenfile (fp1, fp2) to determine whether fp1 and fp2 refer to the same file
os.path.samestat (stat1, stat2) determines stat tuple stat1 and point to the same file stat2
os.path.split (path) and the path into dirname basename, returns a tuple
os.path.splitdrive (path) usually used in the windows, the drive and path returns a tuple
os.path.splitext (path) divided path, return path name and file extension tuple
os.path.splitunc (path) points to load the route into file
os.path.walk (path, visit, arg) traversal path, into each directory function calls the visit, visit function must have three parameters (arg, dirname, names), dirname represents the directory name of the current directory, names represents the current All directory under the file name, args, compared with the third argument of walk
os.path.supports_unicode_filenames set whether to support unicode path name
# coding = 'utf-8'
import os


def rename(path):
    I = 0
     ' to the files of all files (including folder) under ' 
    the FileList = the os.listdir (path)
     ' through all files ' 
    for Files in the FileList:
         ' original file path ' 
        oldDirPath = the os.path.join (path , files)
         ' If the folder is called recursively ' 
        IF os.path.isdir (oldDirPath):
            rename(oldDirPath)
        ' Filename ' 
        fileName = os.path.splitext (Files) [0]
         ' file extension ' 
        fileType = os.path.splitext (Files) [. 1 ]
         ' new file path ' 
        newDirPath = the os.path.join (path , STR (I) + fileType)
         ' rename '
        os.rename(oldDirPath, newDirPath)
        i += 1


path = 'C:\\Users\Administrator\Desktop\AssetScan\\vuln'
rename(path)

Second way:
import the
path = 'C:\\Users\Administrator\Desktop\AssetScan\\vuln'
files = os.listdir(path)
for i, file in enumerate(files):
    NewName = os.path.join(path, "AssetScan_"+file)
    OldName = os.path.join(path, file)
    os.rename(OldName, NewName)

 



Guess you like

Origin www.cnblogs.com/qxh-beijing2016/p/12084150.html