I'm really frustrated with this strange behaviour of Python all of a sudden. I've been writing to files all sorts of data but since today morning it just doesn't seem to work. I've referred to all these before posting:
How to redirect 'print' output to a file using python?
Failed to write to file but generates no Error
Unable to write data into a file using python
Unable to write list of elements to a file using python
Have tried all following commands but it just doesn't write anything to the delete.txt file. What's happening?
fl=open('delete.txt','w')
fl.write(msg) <--Doesnt work,tried this also
fl.write('%s' %msg) <--Doesnt work,tried this also
fl.write("at least write this") <-- Doesnt work,tried this also
print (msg) <- WORKS
Code:
for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')
try:
if len(i)>40:
mes=processComUni(i)
proc=nltk.tokenize.word_tokenize(mes)
#print proc
pos=nltk.pos_tag(proc)
for i in pos:
if ((i[1]=="NN") or (i[1]=="NNP") or (i[1]=="NNS")) and len(i[0])>2:
#print i[0],i[1]
for j in home_depo_inv:
if i[0] in j.split() and (i[0]!='depot' and i[0]!='home' and i[0]!='store' and i[0]!='por' and i[0]!='get' and i[0]!='house' and i[0]!='find' and i[0]!='part' and i[0]!='son' and i[0]!='put' and i[0]!='lot' and i[0]!='christmas' and i[0]!='post'):
a=re.findall(i[0],j)
fl.write(str(i))<--Doesnt work,tried this also
fl.write(str(mes))<--Doesnt work,tried this also
fl.write("\n")<--Doesnt work,tried this also
fl.write("hello")<--Doesnt work,tried this also
fl.flush()
break
except:
continue
fl.close()
More code:
type(mes) = str
mes="omg would love front yard"
Your snippet's indentation is totally messed up, but anyway: your code starts with:
for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')
which means you reopen the file for writing on each iteration, erasing whatever might have been written by the previous iteration.
You need to flush the output stream explicitly when writing to a file handle like that.
f = open("test.txt", "w")
f.write("this is a test\n")
# no text in the output file at this point
f.flush()
# buffers are flushed to the file
f.write("this, too, is a test\n")
# again, line doesn't show in the file
f.close()
# closing the file flushes the buffers, text appears in file
From the documentation of file.write:
Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written.
Related
I am attempting to output a new txt file but it come up blank. I am doing this
my_file = open("something.txt","w")
#and then
my_file.write("hello")
Right after this line it just says 5 and then no text comes up in the file
What am I doing wrong?
You must close the file before the write is flushed. If I open an interpreter and then enter:
my_file = open('something.txt', 'w')
my_file.write('hello')
and then open the file in a text program, there is no text.
If I then issue:
my_file.close()
Voila! Text!
If you just want to flush once and keep writing, you can do that too:
my_file.flush()
my_file.write('\nhello again') # file still says 'hello'
my_file.flush() # now it says 'hello again' on the next line
By the way, if you happen to read the beautiful, wonderful documentation for file.write, which is only 2 lines long, you would have your answer (emphasis mine):
Write a string to the file. There is no return value. Due to buffering, the string may not actually show up in the file until the flush() or close() method is called.
If you don't want to care about closing file, use with:
with open("something.txt","w") as f:
f.write('hello')
Then python will take care of closing the file for you automatically.
As Two-Bit Alchemist pointed out, the file has to be closed. The python file writer uses a buffer (BufferedIOBase I think), meaning it collects a certain number of bytes before writing them to disk in bulk. This is done to save overhead when a lot of write operations are performed on a single file.
Also: When working with files, try using a with-environment to make sure your file is closed after you are done writing/reading:
with open("somefile.txt", "w") as myfile:
myfile.write("42")
# when you reach this point, i.e. leave the with-environment,
# the file is closed automatically.
The python file writer uses a buffer (BufferedIOBase I think), meaning
it collects a certain number of bytes before writing them to disk in
bulk. This is done to save overhead when a lot of write operations are
performed on a single file. Ref #m00am
Your code is also okk. Just add a statement for close file, then work correctly.
my_file = open("fin.txt","w")
#and then
my_file.write("hello")
my_file.close()
I know this question has been asked before quiet a lot on SO and elsewhere too. I still couldn't get it done. And im sorry if my English is bad
Removing file in linux was much more simpler. Just os.remove(my_file) did the job, But in windows it gives
os.remove(my_file)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: (file-name)
my code :
line_count = open(my_file, mode='r') #
t_lines = len(line_count.readlines()) # For total no of lines
outfile = open(dec_file, mode='w')
with open(my_file, mode='r') as contents:
p_line = 1
line_infile = contents.readline()[4:]
while line_infile:
dec_of_line = baseconvert(line_infile.rstrip(),base16,base10)
if p_line == t_lines:
dec_of_line += str(len(line_infile)).zfill(2)
outfile.write(dec_of_line + "\r\n")
else:
outfile.write(dec_of_line + "\r\n")
p_line += 1
line_infile = contents.readline()[4:]
outfile.close()
os.remove(my_file)
Here my_file is a variable that contains complete path structure of a file. Like wise dec_file also contains path, but to a new file. And the file im trying to remove is the file that's being used under read mode. Need some help please.
my try's :
Tried closing the file my_file.close(). The corresponding error i got was AttributeError: 'str' object has no attribute 'close'. I knew, when a file is in
read mode it automatically closes when it comes to the end of the
file. But still i gave it a try
Also tried by os.close(my_file) as per https://stackoverflow.com/a/1470388/3869739. i got error as TypeError: an integer is required
Or, am i getting this error just because i have opened the file
twice (for counting the line and to read the file content),..?
Pythonic way of reading from or writing to a file is by using a with context.
To read a file:
with open("/path/to/file") as f:
contents = f.read()
#Inside the block, the file is still open
# Outside `with` here, f.close() is automatically called.
To write:
with open("/path/to/file", "w") as f:
print>>f, "Goodbye world"
# Outside `with` here, f.close() is automatically called.
Now, if there's no other process reading or writing to the file, and assuming you have all the permission you should be able to close the file. There is a very good chance that there's a resource leak (file handle not being closed), because of which Windows will not allow you to delete a file. Solution is to use with.
Further, to clarify on few other points:
Its the garbage collector that causes the closure of the stream when the object is destroyed. The file is not auto-closed upon complete reading. That wouldn't make sense if the programmer wanted to rewind, would it?
os.close(..) internally calls the C-API close(..) that takes an integer file descriptor. Not string as you passed.
I've already managed to create a program called write.py that accepts information via "post" from HTML and writes this information (only ever one line long) to a file called infoLog.ssv. Now I'm trying to create another .py program that will both append this document with another line of text, and then read everything that is contained within infoLog.ssv and print it to the screen. I seem to be running into issues when my program tries to open the appended file. I've checked and yes, my program is successfully appending the document, but it seems to just stop working when I reopen it.
f=open("infoLog.ssv","a")
strValueQuestion=str(question)
f.write("\n"+strValueQuestion)
f.close()
print "Successfully added"
f1=open("infoLog.ssv","r")
f1.close()
sys.stdout.flush()
print "It never prints this..."
As you can see, while attempting to debug, I only open and close the file now and even still it never prints the last print statement.
Check your logs, it is probably giving an error in the line f1.open("infoLog.ssv", "r").
If you want to print the file contents, what you mean is probably:
with open("infoLog.ssv","r") as f1:
print f1.read()
sys.stdout.flush()
print "It should prints this... =)"
Note that if you use the with statement, you don't need to explicitly close the file descriptor.
Apparently 'r+' or 'w+' allow you to both read and write to a file at the same time.
https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
final=open("war.txt","w+")
for line in madList:
line=line.split('A ')
dnsreg= line[1]
print dnsreg
final.write(dnsreg)
While printing dnsreg I can see the output, but when I write it to a file, nothing is being written. No syntax error is there either. Any idea?
The data written to a file is not written immediately, it's kept in a buffer, and large amounts are written at a time so save the writing-to-disk overhead. However, upon closing a file, all the buffered data is flushed to the disk.
So, you can do two things:
Call final.close() when you are done, or
Call final.flush() after final.write() if you don't want to close the file.
Thanks to #Matt Tanenbaum, a really nice way to handle this in python is to do the writing inside a with block:
with open("war.txt","w+") as final:
for line in madList:
line=line.split('A ')
dnsreg= line[1]
print dnsreg
final.write(dnsreg)
Doing this, you'll never have to worry about closing the file! But you may need to flush in case of premature termination of the program (e.g. due to exceptions).
You should use the with statement in Python when using resources that have to be setup and tear down, like opening and closing of files. Something like:
with open("war.txt","w+") as myFile:
for line in madList:
line=line.split('A ')
dnsreg= line[1]
myFile.write(dnsreg)
If you do not want to use with, you will have to manually close the file. In that case, you can use the try...finally blocks to handle this.
try:
myFile = open("war.txt", "w+")
for line in madList:
line=line.split('A ')
dnsreg= line[1]
myFile.write(dnsreg)
finally:
myFile.close()
finally will always work, so your file is closed, and changes are written.
I'm trying to use a subprocess to write the output to a data file, and then parse through it in order to check for some data in it. However, when I need to do the reading through the file's lines, I always get a blank file unless I close the file and then reopen it. While it works, I just don't like having to do this and I want to know why it happens. Is it an issue with subprocess, or another intricacy of the file mode?
dumpFile=open(filename,"w+")
dump = subprocess.Popen(dumpPars,stdout=dumpFile)
dump.wait()
At this point, if I try to read the file, I get nothing. However, it works fine by doing these commands after:
dumpFile.close()
dumpFile=open(filename,"r")
The with statement automatically closes the file after the block ends:
with open(filename, "w+") as dumpFile:
dump = subprocess.Popen(dumpPars, stdout=dumpFile)
dump.wait()
with open(filename, "r") as dumpFile:
# dumpFile reading code goes here
You probably need to seek back to the beginning of the file, otherwise the file pointer will be at the end of the file when you try to read it:
dumpFile.seek(0)
However, if you don't need to actually store dumpFile, it's probably better to do something like:
dump = = subprocess.Popen(dumpPars,stdout=subprocess.PIPE)
stdoutdata,_ = dump.communicate() #now parse stdoutdata
unless your command produces large volumes of data.
If you want to read what you've already written, either close and reopen the file, or "rewind" it - seek to offset 0.
If you want to read the file while it is being written, you can do so (don't even need to write it to disk), see this other question Capture output from a program