The following is the final code (implemented under Windows)
Copy Code code as follows:
#-*-coding:cp936-*-
Import OS
Path = ' d:\\ picture \ \ '
For file in Os.listdir (path):
If Os.path.isfile (Os.path.join (path,file)) ==true:
If File.find ('. ') <0:
Newname=file+ ' rsfdjndk.jpg '
Os.rename (Os.path.join (Path,file), Os.path.join (Path,newname))
Print file, ' OK '
# Print File.split ('. ') [-1]
This is accomplished by first traversing the directory specified by path, if it is a file (not a folder) and there is no '. ' in the filename, which means that the file is appended with the suffix name, and then renamed
Some details need to be noted:
1. If path Specifies a folder that is not the directory where the program is located, the Rename function must have an absolute path in it, otherwise it will report ' Windowserror: [ERROR 2] ' Error
2. If the new filename already exists in the renaming, it will report ' Windowserror: [ERROR 183] ' Error, so the new filename is best to add some random string
3. If you change the file name or suffix name can be split () function for segmentation
The 4.find function returns '-1 ' if the specified string is not found
Bo Master ma6174