【问题标题】:I am writing a script to scrape a website and download files. After downloading, I am trying to rename the files and it's not working我正在编写一个脚本来抓取网站并下载文件。下载后,我试图重命名文件,但它不工作
【发布时间】:2020-02-08 07:36:18
【问题描述】:
path='C:/Users/ks/Downloads/AU/ORR/'
print (path)
substring = "w"
files = os.listdir(path)
for file in files:
    src=path+file
    if substring in file:
        break
        print ("there")
    else:
        print ("not there")
        dst='Pattern_'+substring+str(org)+str(m1)+'.csv'
        dst=path+dst

    os.rename(src,dst)  

打印语句仅用于调试。一切都在打印,但重命名功能仍然不起作用。

【问题讨论】:

    标签: python-3.x selenium csv web-scraping jupyter-notebook


    【解决方案1】:

    使用break 可以永久退出循环。您可能希望continue 完全跳过此循环迭代。

    substring 首次不在file 中后,您的代码将停止执​​行任何操作。

    break-and-continue-statements-and-else-clauses-on-loops

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2017-09-17
      • 2013-04-05
      • 2011-12-29
      • 1970-01-01
      • 2016-07-16
      相关资源
      最近更新 更多