This question already has answers here:
Confused by python file mode "w+" [duplicate]
(11 answers)
Closed 6 months ago.
I am using VSC for writing python scripts to read/write file. I am able to write file using the below code:
with open("score_card.txt", mode="w+") as file_data:
file_data.write("23")
but when trying to read the file using the below code:
with open("score_card.txt", mode="w+") as file_data:
print(file_data.read())
it is not giving anything in the terminal.
You need to open the file in read mode. Here's an example:
with open("score_card.txt", mode="r") as file_data:
print(file_data.read())
You can learn more about the open function modes here https://docs.python.org/3/library/functions.html#open
An important thing to remember is that the w+ mode is for truncating and writing. This means that it will wipe the file contents, so when you try to read it, there is nothing in the file. You should use mode='r' on all files that you don't intend to edit.
Related
This question already has an answer here:
Why can't I write to a file that I open in python?
(1 answer)
Closed 9 months ago.
I try to open and write something in file, but it gives that mistake:
(me Operation not permitted: '/Users/archi/Desktop/Python/txt.pages)
I have tried to give permission to file, but all that doesn't work.
here is the code
r = open('/Users/archi/Desktop/Python/txt.pages')
r.write('something')
r.close
`
try this:
with open('/Users/archi/Desktop/Python/txt.pages', 'w') as r:
r.write('something')
although i'm not sure how much help it's going to be as you are opening a .pages file which is encoded differently than a standard text file -- it contains a compression with styling information, text, etc.
Are you missing the second parameter for .open()? The default is 'r' which does not allow you to write to the file, in my understanding.
From https://docs.python.org/3/library/functions.html#open:
The default mode is 'r' (open for reading text, a synonym of 'rt'). Modes 'w+' and 'w+b' open and truncate the file. Modes 'r+' and 'r+b' open the file with no truncation.
This question already has answers here:
Difference between modes a, a+, w, w+, and r+ in built-in open function?
(9 answers)
Closed 1 year ago.
When you open file like that:
f = open("demofile.txt", "r")
f.write("Something")
There are some in which you can open file.
"r" Opens a file for reading only.
"r+" Opens a file for both reading and writing.
"rb" Opens a file for reading only in binary format.
"rb+" Opens a file for both reading and writing in binary format.
"w" Opens a file for writing only.
"a" Open for writing. The file is created if it does not exist.
"a+" Open for reading and writing. The file is created if it does not exist.
"x" Creates a new file. If file already exists, the operation fails.
"t" This is the default mode. It opens in text mode.
"b" This opens in binary mode.
"+" This will open a file for reading and writing (updating)
This question already has an answer here:
How can I access an uploaded file in universal-newline mode?
(1 answer)
Closed 8 years ago.
I'm receiving a CSV file via an upload that I would like to parse, but I get Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode? when I try to open it via this line of code:
reader = csv.reader(args['file'], quotechar='|', dialect=csv.excel)
Do I need to save the file temporarily, then open it in universal-newline mode to achieve this? Or is there a way to do so without having to use a temp file?
It seems that you don't need to save it since it returns a reader object where you can iterate on it (just think of working with files).
This question already has answers here:
How do I append to a file?
(13 answers)
Closed 8 years ago.
I need to add my program's outputs to a saved file every time I run the program without overwriting the previous outputs.
This is how I'm writing to a file now:
lead.write(str(alldates))
Any ideas?
Here, you should open the file in append mode:
append_text = str(alldates)
with open('my_file.txt', 'a') as lead:
lead.write(append_text)
There are lots of specialised ways to open files for different purposes, and you can read about them in the documentation.
You should open the file in append mode.
This question already has answers here:
How to print a file to stdout?
(11 answers)
Closed 9 years ago.
I'm trying to get Python to print the contents of a file:
log = open("/path/to/my/file.txt", "r")
print str(log)
Gives me the output:
<open file '/path/to/my/file.txt', mode 'r' at 0x7fd37f969390>
Instead of printing the file. The file just has one short string of text in it, and when I do the opposite (writing the user_input from my Python script to that same file) it works properly.
edit: I see what Python thinks I'm asking it, I'm just wondering what the command to print something from inside a file is.
It is better to handle this with "with" to close the descriptor automatically for you. This will work with both 2.7 and python 3.
with open('/path/to/my/file.txt', 'r') as f:
print(f.read())
open gives you an iterator that doesn't automatically load the whole file at once. It iterates by line so you can write a loop like so:
for line in log:
print(line)
If all you want to do is print the contents of the file to screen, you can use print(log.read())
open() will actually open a file object for you to read. If your intention is to read the complete contents of the file into the log variable then you should use read()
log = open("/path/to/my/file.txt", "r").read()
print log
That will print out the contents of the file.
file_o=open("/path/to/my/file.txt") //creates an object file_o to access the file
content=file_o.read() //file is read using the created object
print(content) //print-out the contents of file
file_o.close()