gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:49:20 25 4
gpt4 key购买 nike

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

例如:

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

相关代码部分:

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:

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

想法?

最佳答案

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

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

希望对您有所帮助。

关于python - 在python中打开单独驱动器上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6046038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com