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.
Related
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.
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.
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
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).
def ConvertFile():
FileNameIn = 'Hexdata.dat'
HexFile = open(FileNameIn, 'r')
for Line in HexFile:
print (Line)
print (Binary(Line))
HexFile.close()
So far I have that, which, when the program is run, converts the Hexidecimal number in the file to binary. This is in a file called Hexdata.dat
What I want to do is then save the binary output into a file called Binarydata.dat
How would I approach this in code? Be aware I'm new with Python and haven't covered this properly. I've tried different bits of code but they've all been unsuccessful, as really, they're all guesses.
I'm not asking you to solve the problem for me, but more asking how I would save the output of a program into a new text file.
You're already most of the way there. You already know how to open a file for reading:
HexFile = open(FileNameIn, 'r')
The 'r' there means "open for reading". If you look at the documentation for the open function, you will see that replacing the r with a w will open a file for writing:
OutputFile = open(FileNameOut, 'w')
And then you can send output to it like this:
print >>OutputFile, "Something to print"
Or use the write method on the file object:
OutputFile.write("Something to print\n")
Read the documentation of the open function (to open the file in write mode) and File Objects (to write information to the opened file).
You have to have 2 files in this script. The one you're reading from and the one you're writing to. Use the option wb (write binary) when opening the file you are going to write into. These two links should help a beginner with little or no Python knowledge complete your exercise: Intro to File Objects and Tutorial on File I/O.
You are currently opening the file in reading mode, so in order to write to the file, you would want to open the file with the buffering mode as ('w'). Quote from: http://docs.python.org. You can do so easily by replacing your 'r' with 'w'.
'w' for writing (truncating the file if it already exists
For more reference see open(name[, mode[, buffering]])
# the file name
FileNameIn = 'Hexdata.dat'
# create a file object: open it with "write" mode
HexFile = open(FileNameIn,"w")
for line in HexFile:
HexFile.write(Binary(line))
HexFile.close()
Have you tried using open('Binarydata.dat', 'w') for writing to the file? There are plenty of ways to write to a file, most of which can be found here: http://docs.python.org/tutorial/inputoutput.html