I would like to write 'yes it does' into a text file I made earlier. when I run my code, it says 'AttributeError: exit'. I was wondering how to remove this error and make it work successfully, thanks for the help.
The code is:
file = ()
def rewrite_test():
open ('testing.txt', 'rb+')
with ('testing.txt'):
print ("yes it does")
rewrite_test()
You can do it like this:
def rewrite_test():
with open('testing.txt', 'w+') as fout:
fout.write('Yes it does.')
Where you had with ('testing.txt'), that would raise an exception because the string 'testing.txt' isn't something that supports the requirements of a with block.
Also you need to open a file for writing not reading, so use 'w' instead of 'r'.
If you don't like using with, you can use the following code:
def rewrite_test() :
f = open('testing.txt', 'w') # You can replace w with a if you want to append
f.write('Yes, it does')
f.close()
rewrite_test()
So it just opens the file, writes to it, and closes it. Also works in Python 2, which doesn't get with. (I am also a Python 2 user, and I don't understand what with does or is.)
Related
I'm python beginner using jupyter-lab and i'm trying to open the text file and want to get the data from text file. Even though it is very simple code, return is weird.
what is problem?????
My code:
import os
f=open("C:/Users/dfdfdfd/Desktop/python/list.txt","rt")
lines = f.readlines
lines
----------------return------------------
<function TextIOWrapper.readlines(hint=-1, /)>
readlines is callable.
with open("C:/Users/dfdfdfd/Desktop/python/list.txt","rt") as f:
lines = f.readlines()
If I run
file = open("BAL.txt","w")
I = '200'
file.write(I)
file.close
from a script, it outputs nothing in the file. (It literally overwrites the file with nothing)
Furthermore, running cat BAL.txt just goes to the next line like nothing is in the file.
But if I run it line by line in a python console it works perfectly fine.
Why does this happen. ( I am a begginner learning python the mistake may be super obvious. I have thrown about 2 hours into trying to figure this out)
Thanks in advance
You aren't closing your file properly. To close it you are missing the () at the end of file.close so it should look like this:
file = open("BAL.txt", "w")
file.write("This has been written to a file")
file.close()
This site has the same example and may be of some use to you.
Another way, especially useful when you are appending multiple values into a single file is to use something like with open("BAL.txt","w") as file:. Here is your script rewritten to include this example:
I = '200'
with open("BAL.txt","w") as file:
file.write(I)
This opens our file with the value file and allows us to write values to it. Also note that file.close() is not needed here and when appending text w+ needs to be used.
to write to a file you do this:
file = open("file.txt","w")
file.write("something")
file.close()
when you use file.write() it deletes all of the contents of the file, if you want to write to the end of the file do this:
file = open("file.text","w+")
file.write(file.read()+"something")
file.close()
There are other ways to do this but this one is the most intuitive (not the most efficient), also the other way tends to be buggy so there is no reason to post it because this is reliable.
Firstly, you're missing the parentheses when you're closing the file. Secondly, writing to a file should be done like this:
file = open("BAL.txt", "w")
file.write("This has been written to a file")
file.close()
Let me know if you have any questions.
I tried to write to a binary (*.bin) file and I met a problem.
When I use the following code, it does not write anything to the file:
abc = str.encode("sabd")
f=open("sbd.bin",'wb')
f.write(abc)
f.close
However, when I use the following code, it works well:
abc = str.encode("sabd")
with open("sbd.bin",'wb') as f:
f.write(abc)
I use Win + Python3.
Instead of f.close, try f.close() to see if that works any better since close() is a method.
I suggest flushing the data to the file, as you are missing this and would cause the file to not be created or written to. e.g. file.flush() will create the file if not existent and write the data to it.
source: https://www.tutorialspoint.com/python3/file_flush.htm and as suggested by cdlane, close the file with file.close() as you are calling a method not getting a variable or something.
I have a python script 'main.py' which calls another python script called 'getconf.py' that reads from a file 'configuration.txt'. This is what it looks like:
if __name__ == "__main__":
execfile("forprolog.py") # this creates configuration.txt
execfile("getconf.py")
When getconf.py is called via main.py it sees configuration.txt as an empty file and fails to read the string from it.
This is how I read from a file:
f1 = open("configuration.txt")
conf = f1.read() #this string appears to be empty
print f1 returns <open file 'D:\\DIPLOMA\\PLANNER\\Exe\\configuration.txt', mode 'r' at 0x01A080D0>
print f1.read() returns an empty string
I suspect the reason of the failure is that the file is being written immediately before calling getconf.py. If I run main.py when configuration.txt is already there it works. Adding a time delay between the actions doesn't solve the problem.
Would appreciate any help!
I saw other questions related to this:
Python read() function returns empty string
Try to add this line before reading:
file.seek(0)
https://stackoverflow.com/a/16374481/4733992
If this doesn't solve the problem, you can still get the lines one by one and add them to a single string:
file = open("configuration.txt", 'r')
file_data = ""
for line in file:
file_data += line
file.close()
I found my problem, it was due to the fact I didn't close the file that I was writing in. Thanks to all who tried to help.
I have created a certain function that is printing text through "print".
i want to store that prompt to a text file.
the function name is "printall(x)".
I have tried the following
text_file = open('newfile.txt', 'w')
text_file.write(printall(x))
text_file.close
this did not work.
how can I make it happen?
Thanks
First, do: import sys at the top of your code.
Then, in your printall(x) method, you should add the following as your first line:
sys.stdout = open('newfile.txt', 'w')
Then, whenever you do print, it will write the code to the file instead of the console.
Hope that helps.