0

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.

2
  • 1
    There is no need to do newdoc.close() - it will close itself once you fall off the end of the with statement. Also, you don't need to use StringIO - csv.reader(datalist, delimiter='\t') is enough. Commented Apr 1, 2012 at 9:26
  • I've taken the close() statement out and it still won't work... Commented Apr 1, 2012 at 9:32

2 Answers 2

4

Where are the input file and where do you want to save the output file. For this kind of scripts i think that it's better use absolute paths

Use:

open('/tmp/lista.txt', 'r') 

instead of:

open('tmp/lista.txt', 'r') 

I think that the error can be related to this

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It could be a path issue, yes. But I don't see any difference between the two examples...
Thank you for the suggestion, I'll try to keep it in mind in the future. The issue is now solved (and it was a problem with the path indeed), see the other answer!
2

It may have something to do with where you start your interpreter.

Try use a absolute path /tmp/randolista.txt instead of relative path tmp/randolista.txt to isolate the problem.

3 Comments

Thanks! OK, it is solved, it was a path issue indeed, and a very stupid one for that. The paths were fine for the interpreter, but not for the folder where the script was placed (which was the same as the input file), so it couldn't find the folder. I sincerely apologise for wasting your time. Thank you all very much!
@JorgeGonzález No worries. SO is all about asking question, there is no need for apology.
Thank you very much! I discovered SO a while ago but had only been reading till now. Fantastic community. Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.