I have written a few lines of code in Python to see if I can make it read a text file, make a list out of it where the lines are lists themselves, and then turn everything back into a string and write it as output on a different file. This may sound silly, but the idea is to shuffle the items once they are listed, and I need to make sure I can do the reading and writing correctly first. This is the code:
import csv,StringIO datalist = open('tmp/lista.txt', 'r') leyendo = datalist.read() separando = csv.reader(StringIO.StringIO(leyendo), delimiter = '\t') macrolist = list(separando) almosthere = ('\t'.join(i) for i in macrolist) justonemore = list(almosthere) arewedoneyet = '\n'.join(justonemore) with open('tmp/randolista.txt', 'w') as newdoc: newdoc.write(arewedoneyet) newdoc.close() datalist.close() This seems to work just fine when I run it line by line on the interpreter, but when I save it as a separate Python script and run it (myscript.py) nothing happens. The output file is not even created. After having a look at similar issues raised here, I have introduced the 'with' parameter (before I opened the output file through output = open()), I have tried flushing as well as closing the file... Nothing seems to work. The standalone script does not seem to do much, but the code can't be too wrong if it works on the interpreter, right?
Thanks in advance!
P.S.: I'm new to Python and fairly new to programming, so I apologise if this is due to a shallow understanding of a basic issue.
newdoc.close()- it will close itself once you fall off the end of thewithstatement. Also, you don't need to useStringIO-csv.reader(datalist, delimiter='\t')is enough.