Austin S
Austin S

Reputation: 33

Adding actual files to a list, instead of just the file's string name

I am having issues reading the contents of the files I am trying to open due to the fact that python believes there is:

"No such file or directory: 'Filename.xrf'"

Here is an outline of my code and what I think the problem may be:

  1. The user's input defines the path to where the files are.

    direct = str(raw_input("Enter directory name where your data is: ))           
    path = "/Users/myname/Desktop/heasoft/XRF_data/%s/40_keV" \
                                                  %(direct)
    print os.listdir(path)
    # This lists the correct contents of the directory that I wanted it to.
    

So here I essentially let the user decide which directory they want to manipulate and then I choose one more directory path named "40_keV".

  1. Within a defined function I use the OS module to navigate to the corresponding directory and then append every file within the 40_keV directory to a list, named dataFiles.

    def Spectrumdivide():
         dataFiles = []
         for root, dirs, files in os.walk(path):
             for file in files:
                 if file.endswith('.xrf'):
                     dataFiles.append(file)
    

Here, the correct files were appended to the list 'dataFiles', but I think this may be where the problem is occurring. I'm not sure whether or not Python is adding the NAME of the file to my list instead of the actual file object.

  1. The code breaks because python believes there is no such file or directory.

    for filename in dataFiles:
        print filename
        f = open(filename,'r')    # <- THE CODE BREAKS HERE
        print "Opening file: " + filename
        line_num = f.readlines()
    

Again, the correct file is printed from dataFiles[0] in the first iteration of the loop but then this common error is produced:

IOError: [Errno 2] No such file or directory: '40keV_1.xrf'

I'm using an Anaconda launcher to run Spyder (Python 2.7) and the files are text files containing two columns of equal length. The goal is to assign each column to a list and the manipulate them accordingly.

Upvotes: 3

Views: 46

Answers (1)

Sede
Sede

Reputation: 61243

You need to append the path name not just the file's name using the os.path.join function.

for root, dirs, files in os.walk(path):
    for file in files:
        if file.endswith('.xrf'):
            dataFiles.append(os.path.join(root, file))

Upvotes: 2

Related Questions