user13553897
user13553897

Reputation:

What is the correct way to write a file path, to access a document in a folder on your computer

I keep getting errors, that the file doesn't exist. This is the first time im doing this and cant find a sufficient answer on google.

the file is stored in a folder with the name : "maths", the documents name is "Data.txt"

the code ive written to read the content is:

def read(data):
    try:
        count=0
        INFILE=open("maths\\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()

Upvotes: 2

Views: 3315

Answers (1)

Red
Red

Reputation: 27577

One sure fire way is to list the whole path of the text file.

Assuming the folder is in Desktop, and the User name of you pc is User1:

def read(data):
    try:
        count=0
        INFILE=open("C:\\Users\\User1\\Usermaths\\Data.txt","r")
        for line in INFILE:
            rawdata.append(line.rstrip())
            count+=1
        INFILE.close()
        return count
    except Exception as e:
        print(e)
        return(count)
        exit()

Upvotes: 1

Related Questions