Differences when accessing a file with Python? - python

Learning about different access methods that can be used when opening a file with Python.
What is the difference between opening a file for both reading and writing vs. opening a file for both writing and reading?
when using the open() function you can use any any of these to open a file:
r
r+
w
w+
a
a+
my question is really would there be a different outcome when using either r+ vs w+ to open files?

The difference between r+ and w+ is that w+ will empty the file first. So you typically use r+ if you want to read the file first, then write to it. You use w+ if you want to write to the file first, then read what you've written.
a+ automatically positions to the end of the file when it's first opened and before every write. So you can use it similarly to r+, but you have to seek first if you want to read what's already in the file, and all writes are appended to the end.
The modes without + just allow reading or writing, not both.

Related

Can I open any file with any extension using python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I am a new coder and I don't know very much about python. While surfing in Internet I found that you can open files through python. So my question is that can I open any file extension/custom file extension through python? Can I also open it as a .txt? Please if you answer, provide the code also! If possible.
So my question is that can I open any file extension/custom file extension through python? Can I also open it as a .txt?
yes and yes!
You can open any text file in python by :
with open("filename.txt") as x:
...
Suppose you want to read the contents of a text file then you could do :
with open("filename.txt", "r") as f:
print(f.read())
This program will print the contents of filename.txt, notice the "r" over there? It is to specify the mode in which you are working with the file (in simple terms, like for read / write)
Also you don't have to mention any mode when you are reading because "r"is the default one but you can be explicit about it
There are a few available 'modes' in which you can open an file :
Read Only (‘r’): Open text file for reading. The handle is positioned at the beginning of the file. If the file does not exist, raises I/O error. This is also the default mode in which the file is opened, as mentioned earlier.
Read and Write (‘r+’): Open the file for reading and writing. The handle is positioned at the beginning of the file. Raises I/O error if the file does not exist
Write Only (‘w’): Open the file for writing. For existing file, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
Write and Read (‘w+’): Open the file for reading and writing. For existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
Append Only (‘a’): Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
Append and Read (‘a+’): Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
You could also open an file by :
name = open(...)
but this generally isn't deemed the best practice because you have to manually close the file later.
So my question is that can I open any file extension/custom file extension through python?
yes. For example you can open an image and read the bytes from it in a similar way mentioned above.

Reading file in "rb" mode while the file is changing

Imagine that you are reading the byte contents of a file in python, with the goal of writing them to a temporary file or bytesio.
What I have not been able to answer is what will happen if say the file is large and while it's open there's a change in the file?
Is there a way to ensure that the file is read correctly, without errors?
I would have dealt with that by simply copying it in the memory first but this doesn't seem to be wise in the scenario of large files.

Python and memory usage when opening files

When opening and appending to a file in python, does that file get loaded into memory? I'm asking this because I'm writing a program where I write to several files in a round-robin fashion where I have the guarantee that any one file can fit into memory but not all files can fit into memory at the same time. Opening and closing files every time I append is not an option since that would be too slow. As such, I would need all the files opened simultaneously.
The answer is NO. Regarding the documentations of open() wraps a system call and returns a file object (Not the content of file): https://docs.python.org/2/library/functions.html#open
Open a file, returning an object of the file type described in section
File Objects.
The file contents are not loaded into RAM unless you read the file with eg.: readlines(), read()

Has .txt file been closed?

I have a slight problem. I have a project that I'm working on and that requires two programs to read/write into some .txt files.
Python writes into one .txt file, C++ reads from it. C++ does what it needs to do and then writes its own information into another .txt file that Python has to read.
What I want to know is how can I check with C++ if Python has closed the .txt file before opening the same file, as Python may still be writing stuff into it and vice versa?
If you need any extra information about this conundrum, feel free to contact me.
whenever, in python, you use:
f.open()
always follow it with
f.close()
then you know its closed
see:
https://docs.python.org/2/tutorial/inputoutput.html
https://www.tutorialspoint.com/python/file_close.htm
re: comment
ETA
How to check if a file has been opened by another application in C++?
Is there a way to check if a file is in use?
C/C++ Standard Function to Check if a file is used by another process?
Way to check in C/C++ if a file is in use?
albeit hacky, I think my favorite after reading through was this one:
if ( 0 != rename("c:/foo.txt", "c:/foo.txt") ) {
printf("already opened\n");
}
https://stackoverflow.com/a/1048721/3680588
You might consider having each "writer" process write its output to a temporary file, close the file, then rename it to the filename that the "reader" process is looking for.
If the file is present, then the respective reader process knows that it can read from it.
Using the with open() method always closes the file after operations are performed. See section 7.2 in the Input and Output documentation.
with open(in_file, 'w') as f:
content = 'Example text'
f.write(content)
Once the above is complete, the file is closed.

python "r+" requires file to exist?

I know that it doesn't make sense to open file for reading if it doesn't exist, unlike for writing. But I need to create a file object, write data to it and then read it later, that's why I want to use the "r+" mode. Of course I can just open the file for writing once and then open the saved file for reading, but the problem is I don't want the file to be saved to disc. Any ideas?
Maybe you should be using a StringIO then. It imitates file-like operations (such as writing to and reading from it).

Categories