What is the best way to open a file as read/write if it exists, or if it does not, then create it and open it as read/write? From what I read, Show
It is not working for me (Python 2.6.2) and I'm wondering if it is a version problem, or not supposed to work like that or what. The bottom line is, I just need a solution for the problem. I am curious about the other stuff, but all I need is a nice way to do the opening part. The enclosing directory was writeable by user and group, not other (I'm on a Linux system... so permissions 775 in other words), and the exact error was:
Neuron 4,5754 gold badges32 silver badges53 bronze badges asked Jun 3, 2010 at 15:05
3 You should use
Igor Chubin 58.5k10 gold badges117 silver badges137 bronze badges answered Jun 3, 2010 at 15:12
muksiemuksie 11.9k1 gold badge18 silver badges14 bronze badges 9 The advantage of the following approach is that the file is properly closed at the block's end, even if an exception
is raised on the way. It's equivalent to
seek() method sets the file's current position.
answered Mar 12, 2013 at 11:06
QwertyQwerty 25.8k21 gold badges101 silver badges124 bronze badges 7
example:
[FYI am using Python version 3.6.2]
bad_coder 9,38619 gold badges37 silver badges61 bronze badges answered Dec 30, 2017 at 16:26
1 Good practice is to use the following:
answered May 4, 2015 at 1:49
lollercoasterlollercoaster 15.1k34 gold badges105 silver badges168 bronze badges 2 Change "rw" to "w+" Or use 'a+' for appending (not erasing existing content) answered Jun 3, 2010 at 15:12
baloobaloo 7,4994 gold badges25 silver badges35 bronze badges 0 Since python 3.4 you should use
Same thing with directories:
answered Apr 23, 2018 at 6:29
GranitosaurusGranitosaurus 19.5k4 gold badges53 silver badges76 bronze badges 6
r+ means read/write answered Jun 3, 2010 at 15:18
KhorkrakKhorkrak 3,8291 gold badge26 silver badges34 bronze badges 3 My answer:
answered May 27, 2014 at 6:20
Chien-Wei HuangChien-Wei Huang 1,7351 gold badge17 silver badges27 bronze badges Use:
Note: Files have to be closed after you open them, and the with context manager is a nice way of letting Python take care of this for you. answered Feb 2, 2015 at 19:36
For Python 3+, I will do:
So, the problem is answered Jan 30, 2021 at 0:27
2
in py3k your code raises
in python-2.6 it raises answered Jun 3, 2010 at 15:11
SilentGhostSilentGhost 293k64 gold badges301 silver badges291 bronze badges What do you want to do with file? Only writing to it or both read and write?
If you need to read from a file, the file has to be exist before open it. You can test its existence before opening it or use a try/except.
Neuron 4,5754 gold badges32 silver badges53 bronze badges answered Jun 3, 2010 at 15:29
user49117user49117 7763 silver badges9 bronze badges 2 I think it's
Neuron 4,5754 gold badges32 silver badges53 bronze badges answered Jun 22, 2013 at 12:16
Put w+ for writing the file, truncating if it exist, r+ to read the file, creating one if it don't exist but not writing (and returning null) or a+ for creating a new file or appending to a existing one. answered Oct 11, 2015 at 20:10
Gustavo6046Gustavo6046 3927 silver badges17 bronze badges If you want to open it to read and write, I'm assuming you don't want to truncate it as you open it and you want to be able to read the file right after opening it. So this is the solution I'm using:
answered Jan 12, 2018 at 13:26
So You want to write data to a file, but only if it doesn’t already exist?. This problem is easily solved by using the little-known x mode to open() instead of the usual w mode. For example:
If the file is binary mode, use mode xb instead of xt. answered Dec 14, 2017 at 13:58
0
answered Aug 8, 2018 at 5:45
When a file that already exists is opened in append mode?When a file that already exists is opened in append mode, the file's existing contents are erased. If you do not handle an exception, it is ignored by the Python interpreter, and the program continues to execute. You can have more than one except clause in a try/except statement.
What happens if you open an output file and the file already exists?In most languages, when you open an output file and that file already exists on the disk, the contents of the existing file will be erased. When an input file is opened, its read position is initially set to the first item in the file.
What will happen when a program opens a file in write mode if the file doesn't exist?If you open a file for writing and the file doesn't exist, then the file is created with 0 length.
What type of file access jumps directly to any piece of data in the file without reading the data that came before it?When working with a sequential access file, you can jump directly to any piece of data in the file without reading the data that comes before it.
|