首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据我输入的列表重命名期末考试

根据我输入的列表重命名期末考试
EN

Stack Overflow用户
提问于 2018-08-28 15:42:19
回答 1查看 53关注 0票数 1

我想根据几个例子来重命名这个文件。我的想法是使用一个列表,例如(示例):

代码语言:javascript
复制
a = [1,2,3,4,5,6,7,8,10, 100, 200]

然后循环到这个列表中,并根据列表中的每个元素进行重命名。

我的代码如下(这是一个示例代码,而不是完整版本,我只是想知道如何将这些元素输入到"**“中,并将其替换为"1”、"2“或"100"):

代码语言:javascript
复制
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")

例如,我希望我的输出是这样的:

代码语言:javascript
复制
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的所有元素。如果可能的话,我就不需要在每次加载新名称时都改变程序中的值了。

EN

回答 1

Stack Overflow用户

发布于 2018-08-28 16:01:49

好吧,

我想你可以为此使用一个环境变量。

代码语言:javascript
复制
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

或者,您可以将当前值保存在文本文件中,并在处理您的内容之前加载它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52052561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档