写文件路径,访问计算机上文件夹中的文档的正确方法是什么

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

我不断收到错误消息,该文件不存在。这是我第一次这样做,无法在google上找到足够的答案。

该文件存储在名称为“ CMPG 111”的文件夹中,文档名称为“ Data.txt”

阅读该内容的代码我是:

def read(data):
    try:
        count=0
        INFILE=open("CMPG 111\\Data.txt","r")
        for line in INFILE:
            rawdata.append(line.rstrip())
            count+=1
        INFILE.close()
        return count
    except:
        print("File could not be found")
        return(count)
        exit()
python data-files
1个回答
0
投票

一种可靠的方法是列出文本文件的整个路径。

假设该文件夹位于桌面中,并且您的PC的用户名为User1:

def read(data):
    try:
        count=0
        INFILE=open("C:\\Users\\User1\\UserCMPG 111\\Data.txt","r")
        for line in INFILE:
            rawdata.append(line.rstrip())
            count+=1
        INFILE.close()
        return count
    except:
        print("File could not be found")
        return(count)
        exit()
© www.soinside.com 2019 - 2024. All rights reserved.