Opening files from different folders

Hi!

I hope this is an okay place for this question, I’ve already finished the Python course, but I just wanted to ask a more detailed question about file input.

<In what way does your code behave incorrectly? Include ALL error messages.>
BACKGROUND: I have a Python project (see below) which turns a series of strings into two columns of numbers for data analysis. The code works perfectly, but only works on files that are in the same folder as my Python file.

<What do you expect to happen instead?>

Is there a way to input the file from another folder? At the moment I have to move each .dat file to the folder containing my .py file, but it would be nicer to keep my .dat files where they are and still be able to input them into my code.



results = open('data.dat', 'r') 

temp = []

for item in results:
    x = item.split()[0] 
    y= item.split()[1] 
    y = y[1:] 
    if float(x) <= -0.8 and float(x) >= -0.95:
        temp.append(y) 
results.close()


Hope someone can help.
Many thanks :slight_smile:

take at the os-module, it should contain everything you need

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.