I have up to 8 seperate Python processes creating temp files in a shared folder. Then I'd like the controlling process to append all the temp files in a certain order into one big file. What's the quickest way of doing this at an os agnostic shell level?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
We can use above code to read all the contents from all the file present in current directory and store into temp.txt file.
Use fileinput:
This is more memory efficient than @RafeKettler's answer as it doesn't need to read the whole file into memory before writing to
big_file
.Simple & Efficient way to copy data from multiple files to one big file, Before that you need to rename your files to (int) eg. 1,2,3,4...etc, Code:
Not aware of any shell-level commands for appending one file to another. But appending at 'python level' is sufficiently easy that I am guessing python developers did not think it necessary to add it to the library.
The solution depends on the size and structure of the temp files you are appending. If they are all small enough that you don't mind reading each of them into memory, then the answer from Rafe Kettler (copied from his answer and repeated below) does the job with the least amount of code.
If reading files fully into memory is not possible or not an appropriate solution, you will want to loop through each file and read them piece-wise. If your temp file contains newline-terminated lines which can be read individually into memory, you might do something like this
Alternatively - something which will always work - you may choose a buffer size and just read the file piece-wise, e.g.
The input/output tutorial has a lot of good info.
Just using simple file IO:
That's about as OS agnostic as it gets. It's also fairly simple, and the performance ought to be about as good as using anything else.
In this code, you can indicate the path and name of the input/output files, and it will create the final big file in that path: