我想根据几个例子来重命名这个文件。我的想法是使用一个列表,例如(示例):
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的所有元素。如果可能的话,我就不需要在每次加载新名称时都改变程序中的值了。
发布于 2018-08-28 16:01:49
好吧,
我想你可以为此使用一个环境变量。
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或者,您可以将当前值保存在文本文件中,并在处理您的内容之前加载它
https://stackoverflow.com/questions/52052561
复制相似问题