Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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-2.7提及url的本地驱动器路径_Python_Python 2.7_Urllib2_Urllib - Fatal编程技术网

如何使用python-2.7提及url的本地驱动器路径

如何使用python-2.7提及url的本地驱动器路径,python,python-2.7,urllib2,urllib,Python,Python 2.7,Urllib2,Urllib,关于链接,我可以检索路径和文件名 进一步,我想把这个文件命名为- D:\Links\filename 例如- -将是原件 D:\Links\category\location\filename.html-这是我希望执行的操作,因此它将作为- fttp://D:/Links/category/location/filename.html 欢迎提供任何形式的帮助/指导 非常感谢:)您可以使用以下内容: import urlparse import os url = "http://www.exam

关于链接,我可以检索路径和文件名

进一步,我想把这个文件命名为-

D:\Links\filename

例如- -将是原件

D:\Links\category\location\filename.html-这是我希望执行的操作,因此它将作为-

fttp://D:/Links/category/location/filename.html

欢迎提供任何形式的帮助/指导


非常感谢:)

您可以使用以下内容:

import urlparse
import os

url = "http://www.example.com/category/location/filename"

url_path = urlparse.urlparse(url).path
local_filename = os.path.join(r'd:', os.sep, 'links', os.path.normpath(url_path)[1:]) + ".html"
print local_filename
这将为您提供一个本地文件名,如下所示:

d:\links\category\location\filename.html

Python函数可用于从URL提取路径,然后该函数可用于修复所有分隔符。最后,可用于将所有部件重新安全连接在一起。

抱歉,这一点非常不清楚。你到底哪里有问题?到目前为止你有什么代码?@DanielRoseman-下面的答案很有效。我只想在解析url时设置本地驱动器路径。对不起,如果我没有足够清楚,虽然我得到了正确的解决方案。这正是完美的工作!谢谢!!!!我不知道os.path.normpath