#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    15
    Rep Power
    0

    "File not open for reading" error


    hi guys,
    I am making a function which has 3 inputs.
    1)a file which looks like this :
    Example 1
    This is line 2
    This is line 3
    2)an output (a new file)
    3) a digit

    what the function should do:
    it should look on the file,
    take a line , multiply it by the digit,
    the put the result in the output. ( one more thing, it should revers the sentences so the first is the last)
    example:
    digit=3
    file:
    Example 1
    This is line 2
    This is line 3
    so when I return the output it should show:
    This is line 3
    This is line 3
    This is line 3
    This is line 2
    This is line 2
    This is line 2
    Example 1
    Example 1
    Example 1

    Code:
    input_path=open("example1.txt","r")
    repeats=3
    output_path=open("new.txt","w")
    def inverse_file(input_path, output_path, repeats):
        
        for line in input_path.readlines():
             for i in range(repeats):
                 output_path.write(line)
        return output_path.read()  
            
    d=inverse_file(input_path, output_path, repeats)
    print d
        
    input_path.close()
    this is my code, and the problem is, when I try to run it , it gives me and error of "File not open for reading"
    and I don't know why.
    could you tell me how I could solve it?
    what is wrong here?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Code:
    return output_path.read()
    You cannot "read" from a file-handle, that you've opened (strictly) for "writing". You may want to look at r+ and w+ options.

    And by the way, you don't have to use nested loops for this.

    Code:
    f_in = open('example1.txt', 'r')
    f_out = open('output.txt', 'w')
    
    lines = f_in.readlines()
    for l in lines[::-1]:
        f_out.write(l * 3)
    
    f_out.close()
    f_in.close()
    Remember, readlines() into a list is not particularly memory efficient if your input file is very huge.
    Last edited by noobie1000; November 16th, 2013 at 07:41 PM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,902
    Rep Power
    481

    Sad


    Such a beautiful post, explaining the function, the input, the output. Such good code design, separating opening the files from the work on the file data.

    And yet the problem seems unrelated to any of this. Check the file access mode from the file system with your shell. Is the file readable and where you expect it?


    Also, the "path" is the sting file name.
    "./example1.txt"

    After opening the file you get a stream.

    input_path="./example1.txt"
    input_stream=open(input_path,"r")
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo