我想根据几个示例重命名文件。 我的想法是通过使用例如(example)的列表:
a = [1,2,3,4,5,6,7,8,10, 100, 200]
然后循环进入此列表并根据列表中的每个元素进行重命名。
我的代码如下(这是代码的示例,而不是完整版本,我只想了解一种将这些元素输入到“ **”并将其替换为诸如“ 1”的值的方法。 “,“ 2”或“ 100”):
class man:
...
def function(a):
...
def output1(myfile):
with open("text_**.txt","w+") as outfile:
outfile.write()
def output2(myfile2):
with open("text_**.txt","w+") as outfile:
outfile.write()
def total(a,b):
with open("text_**.txt","w+") as outfile:
outfile.write()
if __name__ == "__main__":
file = function("**.txt")
...
output1(myfile)
output2(myfile2)
total("**.txt", "**.txt")
例如,我希望输出如下:
class man:
...
def function(a):
...
def output1(myfile):
with open("text_1.txt","w+") as outfile:
outfile.write()
def output2(myfile2):
with open("text_1.txt","w+") as outfile:
outfile.write()
def total(a,b):
with open("text_1.txt","w+") as outfile:
outfile.write()
if __name__ == "__main__":
file = function("1.txt")
...
output1(myfile)
output2(myfile2)
total("1.txt", "1.txt")
我的目标是将“ **”替换为列表中所有元素(从1到200)。如果可以这样做,那么我每次加载时都不需要继续更改程序中的值它的新名称。
答案 0 :(得分:1)
好吧
我想您可以为此使用环境变量。
import os
counter = os.environ.get('MY_FILE_COUNTER')
if counter is None:
os.environ['MY_FILE_COUNTER'] = 1
else:
os.environ['MY_FILE_COUNTER'] += 1
# do your stuff
或者您可以将当前值保存在文本文件中并在处理您的东西之前加载它