[英]How do I rename a file and then find it again in few lines in python?
I use os.rename as so: 我这样使用os.rename:
filename, file_extension = os.path.splitext(o)
x = id_generator()
ll = os.rename(o, x + file_extension)
id_generator()
just makes a random string, and it does rename the file like I want it to, but since os.rename()
doesn't offer anything of value, I can't use it as a variable. id_generator()
只是创建一个随机字符串,并且确实按照我想要的方式对文件进行了重命名,但是由于os.rename()
没有提供任何有价值的信息,因此我不能将其用作变量。 All I need is to be able to find the renamed file in as few lines as possible and be able to set it as a variable. 我需要的是能够在尽可能少的行中找到重命名的文件,并将其设置为变量。
id_generator()
generates a string that has 6 characters and can be numbers and letters. id_generator()
生成一个包含6个字符的字符串,可以是数字和字母。
You could simply store the concatenated string in a new variable before you rename the file. 您可以在重命名文件之前将连接字符串简单地存储在新变量中。
filename, file_extension = os.path.splitext(o)
x = id_generator()
# Store new name before os.rename()
new_file = x + file_extension
os.rename(o, new_file)
Now you can use new_file
in the rest of the logic. 现在,您可以在其余逻辑中使用
new_file
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.