Rename all files's name in Python
# -*- coding:utf-8-*-
import os
import time
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("version", help="Enter your package version")
args = parser.parse_args()
rootdir = os.getcwd()
list = os.listdir(rootdir)
#default version = 1.1.0
version = '1.1.0'
if args.version:
version = args.version
for line in list:
times = time.strftime("%Y%m%d",time.localtime())
list = ['ABCD', 'android', version, 'abcd', times]
st = '_'
result = st.join(list) + '.apk'
origin_file = os.path.join(rootdir, line)
print(origin_file)
new_file = os.path.join(rootdir, result)
print(new_file)
os.rename(origin_file, new_file)
if __name__ == "__main__":
main()
Trying to rename all the file to a specific name in the directory after the script.
However, errors here:
Traceback (most recent call last):
File "rename-apk.py", line 31, in
os.rename(origin_file, new_file)
OSError: [Errno 20] Not a directory
I am guessing you are using Pycharm , .idea
folder is as per documentation -
Project Settings
Project settings are stored with each specific project as a set of xml files under the .idea folder . If you specify the default project settings, these settings will be automatically used for each newly created project.
(Emphasis mine)
Files/Folders with .
in the beginning of the name are by default hidden.
In any way , the .idea
folder is not the issue. The issue would also occur if you tried to rename any other directory.
The issue mainly occurs because of how you create the file name, the times
you use would be constant throughout the execution of the program , since you are only taking the year, month and day.
So basically, you are creating the exact same result name for every file in the list.
The error you got is because you renamed something else to - ABCD_android_1.1.22_abcd_20150903.apk
, which was a file.
Then you are trying to rename .idea
(which is a directory) to ABCD_android_1.1.22_abcd_20150903.apk
which is originally a file , since this is not possible, you get the error - Not a Directory
.
And you may have even lost of alot of .apk
, because of how os.rename
works -
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission.
(Emphasis mine)
You should somehow create unique file_names for each .apk
. Maybe you can include the original filename in the new filename as well , or you should take the timestamp upto the microsecond (You can use datetime module for that, and %f
format for microsecond in it) (even then if the computer is running too fast, you may run into this issue or get errors) .
Also, you may not want to replace all the files, if so you should consider adding some condition to check which files to replace. For example, if you only want to rename .apk
files , you can add a condition like -
if line.endswith('.apk'):
as the first line inside for loop, and the rest of the code inside this if block.
Also, you may not want to use list
as a variable name, since that shadows the built-in function list()
. And you should not re-use same variable names like you are doing right now with list
.
上一篇: 平台的方式来编写Unicode文件
下一篇: 在Python中重命名所有文件的名称