The following is the final code (implemented under Windows)
# -*- coding: cp936 -*-
import os
path = 'D:\\Picture\\'
for file in (path):
if ((path,file))==True:
if ('.')<0:
newname=file+''
((path,file),(path,newname))
print file,'ok'
# print ('.')[-1]
The implementation method is: first traverse the directory specified by path. If it is a file (not a folder) and does not exist in the file name, that is, there is no suffix name, add the suffix name to the file and then rename it.
Some details need to be noted:
1. If the folder specified by path is not the directory where the program is located, the path in the rename function must be an absolute path, otherwise the error "WindowsError: [Error 2]" will be reported.
2. If the new file name already exists during renaming, an error of 'WindowsError: [Error 183]' will be reported. Therefore, it is best to add some random strings to the new file name.
3. If you change the file name or suffix name, you can use the split() function to split it.
If the function cannot find the specified string, it will return '-1'
Blogger ma6174
Copy the codeThe code is as follows:
# -*- coding: cp936 -*-
import os
path = 'D:\\Picture\\'
for file in (path):
if ((path,file))==True:
if ('.')<0:
newname=file+''
((path,file),(path,newname))
print file,'ok'
# print ('.')[-1]
The implementation method is: first traverse the directory specified by path. If it is a file (not a folder) and does not exist in the file name, that is, there is no suffix name, add the suffix name to the file and then rename it.
Some details need to be noted:
1. If the folder specified by path is not the directory where the program is located, the path in the rename function must be an absolute path, otherwise the error "WindowsError: [Error 2]" will be reported.
2. If the new file name already exists during renaming, an error of 'WindowsError: [Error 183]' will be reported. Therefore, it is best to add some random strings to the new file name.
3. If you change the file name or suffix name, you can use the split() function to split it.
If the function cannot find the specified string, it will return '-1'
Blogger ma6174