Good morning friends,

I am having a weird little issue with python that I am certain stems from me failing to understand wtf is going on.

I will give you the general disclaimer that I am a total programming beginner so I'll need some latitude :)

The following snippet is a function I am working on for a larger project that I can't seem to figure out.

The idea is to have python grab all the names of the files in the directory, open them in turn and print the results on screen.

import OS
path = "/home/blahblah/Desktop/projData/"
projData = os.listdir(path)
while (i <= 300):
        for file in projData[i]:
                openedfile = open("/home/blahblah/Desktop/projData/" + file)
                i + 1
                print openedfile

I figured this was pretty straight forward but I was getting I/O errors that the files that it pulled didn't exist.

I've isolated it down to it being when the contents of "file" is appended to the string something funny happens that it doesn't like.

For example, let's say the first file in the directory is named '1', if I run the script I get an I/O error that says '/home/blahblah/Desktop/projData/1' doesn't exist, but if I type the one by hand into '/home/blahblah/Desktop/projData/1' then the file opens just fine.

Obviously there's something I am not understanding! Any help\constructive criticism would be greatly appreciated.

Thanks for your time.

I do not have python here at hand but:

You open a file, and open again without closing it first.
You use a global "file" in a local namespace. Plz do not do that...
There is an endless loop. What is i+1 supposed to meand? i=i+1?
Import OS will raise ImportError...
You store the path in "path" variable, but use it again literally...

Hey thanks for the response, let me clarify:

"You open a file, and open again without closing it first."

technically, I don't actually get the file opened so I'll cross this bridge when I come to it.

"You use a global "file" in a local namespace. Plz do not do that..."
I assume this is a good suggestion, but without a reason I won't learn anything, could you elaborate?


"There is an endless loop. What is i+1 supposed to meand? i=i+1?"
I forgot to declare i as 0 here when I copied over my code, the idea is basic, listdir throws a list, as you probably inferred I want to cycle through that list entry by entry. The loop ends when the condition of the while loop is met.


"Import OS will raise ImportError..."
import os, rather

"You store the path in "path" variable, but use it again literally..."
I tried to recycle variable but when it wouldn't work I replaced it.


Hope those explanations help.

Why are you printing the file handle? I thought you wanted to read what is inside the file.

the way I work on things is in baby steps, if I can get it to open and print the file handle, then I can get it to do what I really want to do later.

means to an end.

Maybe I should refine my question:

How can I send the value that listdir returns into the openfile() string.

Print projData, and type(projData) to see what it is. Read "Basics"in this page especially the use of a for() statement, for some help with using lists. Python naming conventions/style guide.

And dont forget to do before complain python idea.....
Always use try and catch for streams . :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.