【发布时间】:2020-03-26 22:36:38
【问题描述】:
需要帮助
获取文件所在位置的输入并告诉新目录将下载的 URL 保存到 filepath 变量假定带有链接的文本文档位于 python 脚本的位置 - 虽然这有效,但我也希望将其更改为直接。
import os.path
import urllib.request
# Get one line of text (e.g. C:\user\user\path\directory)
filepath = input('please input the url file path: ')
links = open('links.txt', 'r')
newpath = input('Where would you like to store the file? ')
for link in links:`
# then get the filename from the end of the URL
link = link.strip()
filename = link.rsplit('/', 1)[-1]
# Does this file exist in this folder? If not, download it
if not (os.path.isfile(filename)):
print ('Downloading: ' + filename + " to " + newpath)
try:
urllib.request.urlretrieve(link, newpath + filename)
print ("File size was", os.path.getsize(filename))
except Exception as inst:
print (inst)
print (' Encountered unknown error. Continuing.')
# File exists; don't download
else:
print("This file exists already.")
# End of program
print("Finished downloading."
【问题讨论】:
-
请解释问题并告诉我们您真正想要做什么。您当前的描述对于您想要做什么几乎没有意义。
-
我正在尝试创建一个脚本,该脚本采用 .txt 或 .xml 文档(首先以 .txt 开头,以免过于复杂。)用户输入 (.txt) 文件所在的位置位于 - 在我的 python 脚本文件夹之外,并指示程序将下载项目的输出从列表中保存到依赖于用户输入的新文件夹中。
-
好的,可行!当前代码有什么问题,它的行为如何?
-
"C:\Users\Archie\PycharmProjects\List 下载 test\venv\Scripts\python.exe" 请输入 url 文件路径:C:\Users\Archie\Desktop\list 你在哪里喜欢存储文件? C:\Users\Archie\Desktop\outputDL 这个文件已经存在。该文件已经存在。该文件已经存在。下载完毕。进程以退出代码 0 完成,文件在 python 脚本文件夹中下载很好,并给我这个输出(它们当前在那里) newpath 变量似乎没有连接,我不知道如何修复它。跨度>