在Python 3中打开文本文件

问题描述 投票:1回答:1

我不确定这是否已经得到了答案,但我确实看了,找不到任何答案。

我的队友无法通过这条路径访问文件:\\SYDSFILES01\Product\MNL\SellOutQC\PythonScripts\。他们可以访问MNL文件夹中的所有文件,但无法访问\\sydsfiles01\product\。我们所做的是映射最终用N:\SellOutQC\PythonScripts\的文件夹。

但是,当我运行下面的程序时,由于程序试图通过\\SYDSFILES01\Product\MNL\SellOutQC\PythonScripts\找到文件,导致错误。我尝试了一些解决方法,但无济于事。我希望你能帮忙。正在运行的脚本位于N:\SellOutQC\

def import_weekly():
    import csv
    import os
    file = "/PythonScripts/parameters.txt"
    path = os.getcwd()+file

    d={}
    with open(path, 'r+') as file:
        for i in csv.reader(file,delimiter='\t'):
            d[i[0]]=i[1]
    return d

Error: 
PermissionError: [Errno 13] Permission denied: '\\\\sydsfiles01\\product\\manila\\selloutqc\\pythonscripts\\Parameters.txt'
python windows python-3.x file permission-denied
1个回答
0
投票

我假设您的代码使用硬编码路径r'N:\SellOutQC\PythonScripts\Parameters.txt',您可以使用os.popen('cd').readline().strip('\n')读取Windows系统上当前工作目录的映射位置而不是os.getcwd()。例如

mapped_path = os.popen("cd").readline().strip('\n')

在你的情况下,print(mapped_path)应该打印N:\\SellOutQC

© www.soinside.com 2019 - 2024. All rights reserved.