what does 'rb' mean in csv files? [duplicate] - python

This question already has answers here:
What is the difference between rb and r+b modes in file objects [duplicate]
(4 answers)
Closed 6 years ago.
import csv
with open('test.csv','rb') as file:
rows = csv.reader(file,
delimiter = ',',
quotechar = '"')
data = [data for data in rows]
This was in Python: reading in a csv file and saving columns as variables. I couldn't comment, but I'm really confused. What does 'rb' mean?

It means: Read the file in Binary mode.
For a complete list of options view this.

From open() in the Built-in functions documentation:
open(name[, mode[, buffering]])
The most commonly-used values of mode are 'r' for reading, (...) Thus,
when opening a binary file, you should append 'b' to the mode value
to open the file in binary mode, which will improve portability.
So this opens the file to read in a binary mode.

The second argument o open() is the mode the file will be opened in. 'rb' is for Read Binary mode. Read more about it here

Related

Able to write but unable to read file [duplicate]

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.

How can I open file on Mac? Python [duplicate]

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.

What are modes in which you can open file in python? [duplicate]

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)

Using 'w' and 'r' in txt files [duplicate]

This question already has answers here:
How to open a file for both reading and writing?
(4 answers)
Closed 2 years ago.
How can I use the 'r' and 'w' at the same time?
Hi I want to know if I can use 'r' and 'w' at the same time, the following is an example
# Example
with open('txt.txt', 'w') as t:
t.write('Hello World')
t.readable()
eg = t.readlines()
When I write this down it will create the txt file but it gives me an error.
I Want to know how to use 'r' and 'w' at the same time
Please help me
I am suffering
I need help
You use r+. The r character means read-only, w means write-only, r+ means read and write.
Just keep in mind that you'll probably need to manage the file pointers (where your current position is) when switching between read and write operations.

What's the difference between opening a file for update and just appending? [duplicate]

This question already has answers here:
Difference between modes a, a+, w, w+, and r+ in built-in open function?
(9 answers)
Closed 9 years ago.
As in, when opening a file with open(), why do we have modes 'r+', 'w+', 'a+'? Surely mode 'a' does the same job? I'm especially confused by the difference between modes 'a' and 'a+' - could someone explain where they differ and, if possible, when one should use one or the other?
The opening modes are exactly the same that C fopen() std library function.
The BSD fopen manpage defines them as follows:
``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.
``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.
The only difference between a and a+ is that a+ allows reading of files.
See this post for more info.
Quoted from the Python 2.7 tutorial:
mode can be 'r' when the file will only be read, 'w' for only writing
(an existing file with the same name will be erased), and 'a' opens
the file for appending; any data written to the file is automatically
added to the end. 'r+' opens the file for both reading and writing.
The mode argument is optional; 'r' will be assumed if it’s omitted.
'a' opens the file in write (append at end of file) mode, while 'r+' opens it in both read and write (insert at start of file).

Categories