2017-02-20 21:29:26

Hello guys. I have an error that I can't quite fix. I am trying to create a program that will do the following things as of now.
1. open a file and read it.
2. put all the file's information in a list.
3. strip any percent characters from any words in the list.
4. print the list's length.
The problem that I get an IOError: no such file or directory (filename)
here is my code below.If you could, don't write code corrections, as copying coding styles could easily get me a zero on this assignment I am doing for school. Tell me if you can, what is wrong in plain English and tell me roughly what I need to do to correct it.
Thanks.
code:
def read_file(filename):
    wordlist=[]
    with open("{}".format(filename), "rb") as infile:
        infile.read()
        for line in file:
            line.strip()
            line.strip("%")
            wordlist.append(line)
    wordlist=set(wordlist)
    print len(wordlist)
read_file("2of12inf.txt")

2017-02-21 00:36:33

Running your script with a file called "2of12inf.txt" seems to run ok, which would mean that the file your trying to load is either misspelled or not in the same directory as the script thats trying to load it. There's a few problems with your approach as well, strip() will only remove characters at the beginning and end of a string, not at any other point inbetween. To solve this you can iterate over each character in the string and add it to a list, and check if it matches a percent character, skipping it if it does. When you use infile.read() it reads the file and returns it as a string that you can process, but if you don't store that return in a variable it will be lost. You should then iterate over that returned string instead of infile.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2017-02-21 01:41:50

That's the purplexing part. It is in the same directory as my script: my desktop. Why else could I be getting that error?

2017-02-21 03:16:56 (edited by magurp244 2017-02-21 03:18:42)

Hm, well if thats the case then python may be having difficulty recognizing the path to the file. You can get the full path like so:

os.path.join(os.path.abspath("."), "2of12inf.txt")

Just use it when loading the file.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2017-02-21 07:55:16

Regarding the filename part, are you specifying the name as a program parameter? If so, spaces could be breaking it.

I'm not sure if the forum is removing part of the code, but that code should be throwing runtime errors.

    with open("{}".format(filename), "rb") as infile:
        infile.read()

The infile.read() here is redundant because <file object>.read() returns the entire content of the file as a string. This isn't allocated to anything so it will just read the file and do nothing. You don't really need to format a string here either since there isn't really any other text in the string, you can just call with open(filename, "r") as infile instead. Also binary and textfiles are different, I recommend just going with the default "r" file mode. This is actually default so you don't even need to specify the second parameter of "r".

for line in file:

your input file is named infile. The type of the file object is file, so that code is literally trying to iterate over the type object of a file.

If you do for line in infile, you may end up with some strange blank lines and characters, I don't really know why. Python actually has a readlines method on file objects so you can just call lines = infile.readlines() for example then iterate through those.

You can use <string>.replace(original, replacement) to replace characters. This also lets you replace them with the empty string. So you can just do line.replace("%", "") to remove all instances of a % character in a string.

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2017-02-21 19:33:55

i thing python is a cool language.

If that's helpfull, why don't you press Thumbs up?