python "r+" requires file to exist? - python

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).

Related

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()

In Python, is it possible to let a subprocess write to an in-memory bytes object instead of a file?

I have a process that I want to run, which does conversion of files into pdfs. I'm using Libreoffice for that. I call Libreoffice as a subprocess, from Python. Libreoffice then writes to a new file on my system. This is of course totally independent of my Python program. After the conversion I will read the file and then use this file for something else, in Python.
But is it at all possible to capture this file as a bytes object in Python instead of a file? This would eliminate the need for reading the file after the conversion, and just keeping it in memory.

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 Tkinter tkFileDialog

in short, what's the difference between
tkFileDialog.asksaveasfile
and
tkFileDialog.asksaveasfilename
I could not understand from the build in docs
asksaveasfile asks the user for a file, then opens that file in write mode and returns it to you so you can write in it.
asksaveasfilename asks the user for a file, then returns that file's name. No file is opened; if you want to write to the file, you'll have to open it yourself.
asksaveasfilename might be preferred over asksaveasfile if you want to do something fancier to the file than just writing data to it. For instance, you might want to first copy the file to another directory as a backup. In which case, you'd prefer to get just the file name so you can perform the copy without having to worry about whether having the file open will cause the copy to fail.
According to the http://tkinter.unpythonic.net/ wiki:
Similar to:
First you have to decide if you want to open a file or just want to get a filename in order to open the file on your own. In the first case you should use tkFileDialog.askopenfile() in the latter case tkFileDialog.askopenfilename().
then:
Saving files works in a similar way. You also have two variants of the function, one to get an opened file which you can use to save your data and another to get a file name in order to open the file on your own. These functions are only provided in the single file version. A multiple file version would make no sense.

Categories