windows 在 python 中的单独驱动器上打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6046038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:52:19  来源:igfitidea点击:

Open file on separate drive in python

pythonwindowsfile-io

提问by Spencer Rathbun

I have an annoying problem in python 2.7 on windows XP. I've got some code that collects a file name off the command line with the argparse library. I then try and open said file. Normally, this works fine, and if you pass in a full path name it successfully opens that too. However, if the path uses a drive letter other than the location you started from, python fails with an IO error, stating that the file or directory does not exist.

我在 Windows XP 上的 python 2.7 中有一个烦人的问题。我有一些代码可以使用 argparse 库从命令行中收集文件名。然后我尝试打开所述文件。通常,这工作正常,如果您传入完整路径名,它也会成功打开它。但是,如果路径使用的驱动器号不是您开始的位置,python 将失败并显示 IO 错误,指出文件或目录不存在。

For example:

例如:

C:\>schema_split.py "C:\path\to\file"
works!
C:\>schema_split.py "I:\path\to\file"
fails!

Relevant code section:

相关代码部分:

parser = argparse.ArgumentParser(description='Process the Accounting file.', version='%(prog)s 1.1')
parser.add_argument('infile', nargs="+", type=str, help='list of input files')
# get the current arguments and put them into a variable
args = parser.parse_args()
for f in args.infile:
    with open(f, "rb") as mycsv:

I don't know why python would have problems with alternate drive letters. The only thing I can come up with is that we run it on a shared drive mapped to a local drive. But for all intents and purposes, the program shouldn't "see" the fact that it is operating on a remote drive.

我不知道为什么 python 会遇到替代驱动器号的问题。我唯一能想到的是我们在映射到本地驱动器的共享驱动器上运行它。但出于所有意图和目的,程序不应该“看到”它在远程驱动器上运行的事实。

Thoughts?

想法?

采纳答案by Steven Rumbalski

You are assuming python is having problems with drive letters. It isn't. Your problem is something else.

您假设 python 在驱动器号方面存在问题。不是。你的问题是别的。

C:\>python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open(r"U:\foo.txt")
>>> 

As you can see opened a file from another drive using backslashes without error.

如您所见,使用反斜杠从另一个驱动器打开了一个文件,没有错误。

Use the following script to diagnose your problem:

使用以下脚本来诊断您的问题:

import os
import sys

path = sys.argv[1]
basepath, fname = os.path.split(path)
print "directory:", basepath
if os.path.exists(basepath):
    print "directory exists"
else:
    print "directory does not exist!"
    sys.exit()

if not fname:
    print "no filename provided!"
    sys.exit()
print "filename:", fname
if os.path.exists(path):
    print "filename exists"
else:
    print "filename not found!"
    print "directory contents:"
    for fn in os.listdir(basepath):
        print fn

Pass your path to the script and it will test the path and file name you pass to it.

将您的路径传递给脚本,它将测试您传递给它的路径和文件名。

回答by wilbbe01

I think you may want to try two slashes instead of 1. Also I think this SO Questionmight be helpful to you.

我认为您可能想尝试使用两个斜杠而不是 1。另外,我认为这个 SO Question可能对您有所帮助。

Two slashes like this C:\>schema_split.py "I:\\path\to\file"

像这样的两个斜线 C:\>schema_split.py "I:\\path\to\file"

Hope this is helpful.

希望这是有帮助的。

回答by Cédric Julien

You can use the os.path.normpathto normalize path and maybe check if the path is valid.

您可以使用os.path.normpath来规范化路径,并可能检查路径是否有效。