0

im writing this function to read from a file and write to another output file, but i recieve no output. Did I write these functions correctly? if they are correct the problem lies within the main body of the decode function, which i will try and sort out.

the code:

def ReadFile():                                                                 #Reads data from file
    try:
        count=0
        stringvar=INFILE.open("K:\Data.txt","r")
        for line in INFILE:
            mylist.append(line.rstrip())
            count+=1
        INFILE.close()
        return count
    except:
        print("File could not be found")
        exit()


writing output file code:

def WriteFile(outlist):                             #outputs data to output list
    OUTFILE=open("Output.txt","a")
    for details in outlist:
        OUTFILE.write(details+"/n")
        parselist.append(a+": Was issued by " +b+ " in "+c+".""The card expires on "+d1+"/"+d2+".The card i linked to" +e+ "with account number:" +f)
    OUTFILE.close()

Is there any problem with the code above?

if it will help i will post the whole code ive written.

  • 3
    Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – Rishabh Agrahari Jun 28 '20 at 16:50
  • Have you tried printing the contents of `mylist` after file reading and `outlist` before file writing? It might help to see if the file is actually read and decoding is done correctly. – Moosa Saadat Jun 28 '20 at 16:51
  • Not sure why you have `INFILE.open`? It should just be `open(your_file, "r")`. – ddastrodd Jun 28 '20 at 16:52
  • ill try that quickly – user13784854 Jun 28 '20 at 16:52
  • btw: It is better to use a *with* statement, e.g.: `with open(, , ...) as opened_file: `. So you can define inside a block what is needed to be done and don't need to close the file at the end. All is done for you by the used context manager under the hood. – colidyre Jun 28 '20 at 16:56
  • Does this answer your question? [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) – colidyre Jun 28 '20 at 17:00

2 Answers2

1

You usually write to files using the with keyword. We do this so that we can call context managers. Through the use of dundermethods or magic methods we can specify what happens when and if the file does not write or read correctly, so that we'll always be able to close said file if the code fails before being able to do so. Look at this example:

class File:
def __init__(self, file_name, method):
    self.file = open(file_name, method)

def __enter__(self):
    print("Enter")
    return self.file

def __exit__(self, type, value, traceback):
    print("Exit")
    self.file.close()

with File("file.txt", "w") as f:
print("Middle")
f.write("hello!")   #Even if there is an exception and/or we didn't specify a file.close, 
                    #it does so because of the dundermethod we defined in the class.
  • Please include your code as (properly formatted) text in your answer. It's impossible to copy and paste from an image of code, so nobody is likely to every run your example. – Blckknght Jun 28 '20 at 17:00
  • @Blckknght this better? –  Jun 29 '20 at 18:35
  • It is improved! The indentation is a little bit messed up, if you fix that it will all be good. – Blckknght Jun 29 '20 at 18:59
0

this is easy fix, just try doing this:

def WriteFile(outlist):                             #outputs data to output list
    with open("Output.txt","a") as OUTFILE:
        for details in outlist:
            OUTFILE.write(details+"/n")
            parselist.append(a+": Was issued by " +b+ " in "+c+".""The card expires on "+d1+"/"+d2+".The card i linked to" +e+ "with account number:" +f)

Just use the with open(...) as ... and it will work, if it throws an error you can try doing: OUTFILE.write(str(details)+"/n") and it will work. hope it helps!

galfar
  • 36
  • 6