Python - 如何从服务器读取路径文件/文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4163456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - how to read path file/folder from server
提问by newbie pisan
Using Python, how might one read a file's path from a remote server? This is a bit more clear to me on my local PC.
使用 Python,如何从远程服务器读取文件路径?在我的本地 PC 上,这对我来说更清楚一些。
回答by Johnsyweb
See Reading and Writing Filesin the Python Tutorial, which is a great place to start for a newbie.
请参阅Python 教程中的读取和写入文件,这是新手入门的好地方。
Be sure to escape your backslashes on Windows, viz:
一定要在 Windows 上转义你的反斜杠,即:
f=open('\\SERVER\share\file.ext', 'r')
or use "raw" strings:
或使用“原始”字符串:
f=open(r'\SERVER\share\file.ext', 'r')
回答by Adrien Plisson
use the os.pathmodule to manipulate path string (you need to import os)
使用os.path模块来操作路径字符串(你需要import os)
- the current directory is
os.path.abspath(os.curdir) - join 2 parts of a path with
os.path.join(dirname, filename): this will take care of inserting the right path separator ('\' or '/', depending on the operating system) for building the path
- 当前目录是
os.path.abspath(os.curdir) - 将路径的 2 部分与
os.path.join(dirname, filename): 这将负责插入正确的路径分隔符('\' 或 '/',取决于操作系统)以构建路径

