如何使用Python下载和重写文件?

弗兰克·C

代码的链接在这里(请不要在此处复制以表示感谢):

我不希望它像现在这样用日期更改名称,而是要下载文件“ finviz.csv”并每天(使用调度程序任务)重写它,以使数据在我的数据系统中保持更新。

我尝试了一些调整,但是我不是开发人员,我不知道如何执行此操作。你能帮忙吗?

颜菲利

代码中的注释非常清楚地描述了它:

# we're going to name the file by the date it was downloaded (e.g. 2012-3-18.csv)
fname = now.strftime("%Y-%m-%d")+".csv";

因此,只需将行更改为

fname = "finviz.csv";

并修复文件存在检查逻辑:

# check if the file does not already exist
if not os.path.isfile(savepath+"/"+fname):
    # open a file to save the data to ("wb" means write binary mode)
    outfile = open(savepath+"/"+fname, "wb");
    # download the data from the url specified above
    infile = urllib2.urlopen(url);
    # read the downloaded data and write it to our output file
    outfile.write(infile.read());
    # close the output file once we're done
    outfile.close();
else:
    print "'"+fname+"' ALREADY EXISTS in the save directory '"+savepath+"'.";

到:

# open a file to save the data to ("wb" means write binary mode)
outfile = open(savepath+"/"+fname, "wb");
# download the data from the url specified above
infile = urllib2.urlopen(url);
# read the downloaded data and write it to our output file
outfile.write(infile.read());
# close the output file once we're done
outfile.close();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用python从网站下载多个文件和图像

如何使用Python,Selenium和PhantomJS下载文件

使用certutil和Python下载文件

如何使用 Python 下载多个 PDF 文件?

如何使用python从git下载html文件?

如何使用Python从GeoServer下载GeoTiff文件

如何使用PHP搜索和重写CSV文件?

如何使用nginx下载时创建动态重新命名PDF文件的重写规则?

如何使用Selenium和python禁用Firefox中的文件下载弹出窗口?

在 Python 中使用 get 和 requests 时如何防止下载空的 pdf 文件?

如何使用pyQt 5网络引擎和python脚本代码下载文件?

如何使用Python和Drive API v3下载Google云端硬盘文件

如何使用 Python 和 Drive API v3 从 Google Drive 下载文件

如何使用python请求URL和下载文件夹?

如何使用 Python 从给定 SAS URI 和容器名称的 Azure Blob 存储下载文件列表?

如何停止 Python 重写文件?

使用Python就地重写文件

如何使用Web Automation和Selenium跟踪文件的下载?

使用scrapy抓取后如何压缩和清理下载的文件

如何使用Java从Internet下载和保存文件?

如何使用Java和Jersey存储已下载的文件?

如何使用GeckoDriver Firefox和Selenium下载文件?

如何验证使用Spring SFTP下载的文件的校验和

如何使用Retrofit和Kotlin协程下载PDF文件?

如何使用node.js和http下载文件?

如何使用QWebEngineView和QUrl下载CSV文件

如何使用Cloud Composer下载和访问文件?

如何使用laravel和vuejs下载文件

如何使用ServiceStack和Angular下载动态生成的XML文件?