简体   繁体   English

在Python 3中打开文本文件

[英]Opening a text file in Python 3

I'm not sure if this has been answered already but I did look and can't find any. 我不确定是否已经回答了这个问题,但是我确实找到了,找不到任何东西。

My teammates does not have access to a file via this path: \\\\SYDSFILES01\\Product\\MNL\\SellOutQC\\PythonScripts\\ . 我的队友无法通过以下路径访问文件: \\\\SYDSFILES01\\Product\\MNL\\SellOutQC\\PythonScripts\\ They have access to all files within the MNL folder but cannot access \\\\sydsfiles01\\product\\ . 他们可以访问MNL文件夹中的所有文件,但不能访问\\\\sydsfiles01\\product\\ What we did is to map the folder ending up with N:\\SellOutQC\\PythonScripts\\ . 我们要做的是映射以N:\\SellOutQC\\PythonScripts\\结尾的文件夹。

However, when I run the below, it results to an error as the program tries to locate the file via \\\\SYDSFILES01\\Product\\MNL\\SellOutQC\\PythonScripts\\ . 但是,当我运行以下命令时,由于程序尝试通过\\\\SYDSFILES01\\Product\\MNL\\SellOutQC\\PythonScripts\\查找文件,因此导致错误。 I tried some workaround but to no avail. 我尝试了一些解决方法,但无济于事。 I hope you could help. 希望您能提供帮助。 The script being run is within N:\\SellOutQC\\ 正在运行的脚本位于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'

I assume that your code is working with the hardcoded path r'N:\\SellOutQC\\PythonScripts\\Parameters.txt' , you can use os.popen('cd').readline().strip('\\n') to read the mapped location of current working directory on Windows system instead of os.getcwd() . 我假设您的代码使用的是硬编码路径r'N:\\SellOutQC\\PythonScripts\\Parameters.txt' ,您可以使用os.popen('cd').readline().strip('\\n')进行读取Windows系统上当前工作目录而不是os.getcwd()的映射位置。 eg 例如

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

The print(mapped_path) should print N:\\\\SellOutQC in your case. 在您的情况下, print(mapped_path)应该打印N:\\\\SellOutQC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM