Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从路径'下载;文件';保存到';其他目录';python脚本问题_Python_Url_Save_Os.path - Fatal编程技术网

从路径'下载;文件';保存到';其他目录';python脚本问题

从路径'下载;文件';保存到';其他目录';python脚本问题,python,url,save,os.path,Python,Url,Save,Os.path,需要帮助吗 获取文件所在位置的输入并告诉新目录将下载的URL保存到 filepath变量假定带有链接的文本文档位于python脚本的位置—虽然这样做有效,但我也希望将其更改为direct 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

需要帮助吗

获取文件所在位置的输入并告诉新目录将下载的URL保存到 filepath变量假定带有链接的文本文档位于python脚本的位置—虽然这样做有效,但我也希望将其更改为direct

    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."


如果您的错误是关于为什么
新路径
似乎没有连接到它。这是因为我相信,在这行代码
urllib.request.urlretrieve(link,newpath+filename)
中,
链接
参数并不代表任何东西。您可以尝试打印该值,并查看它打印的内容

请注意,for循环中的
link
是一个局部变量,其中保存的任何内容都无法在代码中的任何其他地方访问

for link in links:`
   # then get the filename from the end of the URL
   link = link.strip()
   filename = link.rsplit('/', 1)[-1]
尝试在循环外部定义名为
link
的全局变量,以便保存在循环中的值可以在代码中的任何地方使用。 这样,


请解释这个问题,并告诉我们你到底想做什么。您当前的描述对您想要做的事情没有多大意义。我正在尝试创建一个脚本,该脚本接受.txt或.xml文档(首先从.txt开始,以免使其过于复杂。)用户输入(.txt)文件所在的位置-在我的python脚本文件夹之外,并指示程序将列表中下载项目的输出保存到依赖于用户输入的新文件夹中。好的,可行!当前代码有什么问题,它的行为如何?“C:\Users\Archie\PycharmProjects\List download test\venv\Scripts\python.exe”请输入url文件路径:C:\Users\Archie\Desktop\List您想将文件存储在哪里?C:\Users\Archie\Desktop\outputDL此文件已存在。此文件已存在。此文件已存在。下载完毕。进程结束,退出代码为0。文件在python脚本文件夹中下载良好,并给我此输出(它们当前在那里)。newpath变量似乎没有连接,我不知道如何修复它。“C:\Users\Archie\PycharmProjects\List download test\venv\Scripts\python.exe”请输入url文件路径:C:\Users\Archie\Desktop\list您希望将文件存储在哪里?C:\Users\Archie\Desktop\outputDL下载:aardvark\u 014.jpg到C:\Users\Archie\Desktop\outputDL[WinError 2]系统找不到指定的文件:“aardvark\u 01.jpg”遇到未知错误。持续的。下载完毕。这是当我将其定向到C:\outputDL时得到的输出,如果我将第一个输入留空,并将其正确下载到python脚本文件夹中的列表。您是否可以将
print('Downloading:'+filename+“to”+newpath)+替换为
print('Downloading:'+filename+“to”+newpath+“+链接)
并显示输出plz“C:\Users\Archie\PycharmProjects\List download test\venv\Scripts\python.exe”请输入url文件路径:C:\Users\Archie\Desktop\List您想将文件存储在哪里?C:\Users\Archie\Desktop\outputDL下载:aardvark\u thumb.jpg到C:\Users\Archie\Desktop\outputDL[WinError 2]系统找不到指定的文件:“aardvark\u thumb.jpg”遇到未知错误。继续.Traceback(最后一次调用):文件“C:/Users/Archie/PycharmProjects/List download test/Downloadlist.py”,第5行,links=open('url.txt','r')FileNotFoundError:[Errno 2]没有这样的文件或目录:“url.txt”#如果我更改了python库根目录中未保存的.txt的名称,我会收到此错误,这可能是我缺少关键代码的地方?#您可以尝试执行此
urllib.request.urlretrieve(“https://www.nationalgeographic.com/content/dam/animals/thumbs/rights-exempt/mammals/a/aardvark_thumb.JPG")
然后看看它是否有效?这可能是因为它需要一个完整的url,而您没有提供。如果问题仍然存在,请参阅以了解替代方案
filepath = input('please input the url file path: ')
links = open('links.txt', 'r')
newpath = input('Where would you like to store the file? ')
link = ""