【问题标题】:Python Download Files in Linux- How to Not Replace File with the Same Name?Linux中的Python下载文件-如何不替换同名文件?
【发布时间】:2019-04-04 03:50:35
【问题描述】:

我创建了一个 python 脚本,它将在 Linux 环境中使用 selenium 和 Chrome 浏览器从站点下载文件。由于我一次运行几个实例,我遇到了丢失旧文件的问题。旧文件通常会被新文件替换,因为它们是同时下载的。

在windows中,文件将被保存为filename(1), filename(2)..等等。但特别是在 Linux 中并非如此。 我该如何解决这个问题? (有什么办法可以改变Chrome的坐姿来禁用替换功能吗?)

注意: 我已经有一个工作功能,可以在下载完成后立即重命名下载的文件。但是,有时两个文件会同时完成下载,并且只会保留一个。

def rename():
    try:
        old_file = os.path.join(str(dlPath), "ClientReport.xlsx")
        new_file = os.path.join(str(dlPath), "ClientReport" + str(int(time.time())) + str(float(random.randint(0,100) + random.randint(1,99))/100) +'.xlsx')
        os.rename(old_file, new_file)
    except:
        print('No download to rename')

非常感谢您的帮助!

【问题讨论】:

    标签: python linux download selenium-chromedriver rename


    【解决方案1】:

    先检查文件是否存在:

    if not os.path.exists(path):
        # Your copying logic
    else:
        print('file not copied because it was already there') # something like this.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-20
      • 2021-01-28
      • 2014-12-23
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多