如何将文件保存到python中的特定目录? [英] How to save a file to a specific directory in python?
本文介绍了如何将文件保存到python中的特定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我正在使用此代码来保存下载的文件,但它会将它们放在运行它的同一文件夹中.
r = requests.get(url)with open('file_name.pdf', 'wb') as f:f.写(r.内容)
如何将下载的文件保存到我选择的另一个目录?
解决方案
或者如果在 Linux 中,请尝试:
# 保存到绝对路径.r = requests.get(url)with open('/path/I/want/to/save/file/to/file_name.pdf', 'wb') as f:f.写(r.内容)# 保存到相对路径.r = requests.get(url)with open('folder1/folder2/file_name.pdf', 'wb') as f:f.写(r.内容)
请参阅open() 函数文档了解更多详情.>
Currently, I am using this code to save a downloaded file but it is placing them in the same folder where it is being run from.
r = requests.get(url)
with open('file_name.pdf', 'wb') as f:
f.write(r.content)
How would I save the downloaded file to another directory of my choice?
解决方案
Or if in Linux, try:
# To save to an absolute path.
r = requests.get(url)
with open('/path/I/want/to/save/file/to/file_name.pdf', 'wb') as f:
f.write(r.content)
# To save to a relative path.
r = requests.get(url)
with open('folder1/folder2/file_name.pdf', 'wb') as f:
f.write(r.content)
See open() function docs for more details.
这篇关于如何将文件保存到python中的特定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文