【问题标题】:how to not overwrite file in python in different os path如何在不同的操作系统路径中不覆盖python中的文件
【发布时间】:2022-12-02 17:14:05
【问题描述】:

so i want to avoid overwrite the file name that existed. but i don\'t know how to combine the code with mycode. please help me

here\'s my code for write file:

def filepass(f):
    print(f)
    with open (\'media/pass/\'+\'filepass.txt\', \'a\') as fo:
        fo.write(f)
        fo.close()
    return fo

and here\'s the code to create number in name filepass:

def build_filename(name, num=0):
    root, ext = os.path.splitext(name)
    print(root)
    return \'%s%d%s\' % (root, num, ext) if num else name

def find_next_filename(name, max_tries=20):
    if not os.path.exists(name): return name
    else:
        for i in range(max_tries):
            test_name = build_filename(name, i+1)
            if not os.path.exists(test_name): return test_name
        return None

all i want is to create filename : filepass.txt, filepass1.txt, filepass2.txt

  • Uh, what is your question? How to call find_next_filename?
  • how to use the func build_filename and find_next_filename into func filepass? all i want just to create filename : filepass.txt, filepass1.txt, filepass2.txt @tripleee

标签: python file filenames


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 2019-03-01
  • 1970-01-01
相关资源
最近更新 更多