将文件从URI格式路径复制到本地路径

2024-09-29 19:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图复制服务器上的一个文件,但我得到的只是它的URI格式路径。
我一直在尝试在C#.NET 4.5中实现复制,但似乎CopyFile不适合处理URI格式。
所以我在shutil中使用了IronPython,但似乎它也不适合URI格式路径。在

如何在本地获取该文件?在

private string CopyFile(string from, string to, string pythonLibDir, string date)
{
    var dateTime = DateTime.Today;
    if (dateTime.ToString("yy-MM-dd") == date)
    {
       return "";
    }
    var pyEngine = Python.CreateEngine();
    var paths = pyEngine.GetSearchPaths();
    paths.Add(pythonLibDir);
    pyEngine.SetSearchPaths(paths);
    pyEngine.Execute("import shutil\n" +
                     "shutil.copyfile('" + from + "', '" + to + "')");
    return dateTime.ToString("yy-MM-dd");
}

我从xml配置文件获取所有路径。在


Tags: 文件tofrom路径datetimedatestringvar
2条回答

用Python

import urllib
urllib.urlretrieve("http://www.myserver.com/myfile", "myfile.txt")

^{}

Copy a network object denoted by a URL to a local file, if necessary. If the URL points to a local file, or a valid cached copy of the object exists, the object is not copied.

您可以使用webclient,然后在特定文件夹中获取文件。在

using (WebClient wc = new WebClient())
    wc.DownloadFile("http://sitec.com/web/myfile.jpg", @"c:\images\xyz.jpg");

或者,您也可以使用:HttpWebRequest,因为您只想从服务器读取文件中的内容。在

^{pr2}$

相关问题 更多 >

    热门问题