1

so i have gone from a linux working environment to windows during the pandemic working from home.

I am using what was the family computer so cant really change to linux on it.

I am trying to combine two files into a third file on linux i can do this using the following

cat file1 file2 > file3

in python i can do this using subprocess this worked fine at my office

on windows i was told it can be done using type with the following command

type file1 file2 > file3

if i run this manually via cmd it works fine and results in a 7mb file but if i try and run the command using subprocess in python the result is a 165byte file

if i print the stdout i simply get the following

b''

the function i am using to run the sub process is as follows

def RUN_SUBPROCESS(COMMAND, LOG):

print('#### STARTING SUB PROCESS')
SUB_PROCESS = subprocess.Popen(COMMAND, shell=True, stdout=subprocess.PIPE)
out, err = SUB_PROCESS.communicate()

SUB_PROCESS.wait()
if LOG == 'true':
    print('#### OUTPUT')
    print(out)
    print('#### ERROR')
    print(err)

print('#### SUB PROCESS COMPLETE')
        
return
user72261
  • 11
  • 1
  • 5
  • 1
    Please open a [command prompt](https://www.howtogeek.com/235101/), run `copy /?` and read the output help. What you want to achieve could be done with `copy /B file1+file2 file3`. The option `/B` (binary) is important in this case as otherwise the EOF (end of file) control character would be appended by internal command __COPY__ of `cmd.exe`. But I don't understand why in a Python script the shell interpreter of the operating system is used to merge two files together into a third file. This can be done with native Python code quite easily too and would therefore work on any operating system. – Mofi Oct 25 '21 at 16:22
  • 2
    BTW: `shell=True` results on Windows in using the Windows kernel library function [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) with value of environment variable `ComSpec` being defined with `%SystemRoot%\System32\cmd.exe` which is usually `C:\Windows\System32\cmd.exe` with first argument being the option `/c` and the other arguments from call of `subprocess.Popen`. Run in the command prompt window `cmd /?` and read how the argument(s) after `/c` are interpreted by `cmd.exe`. Operator `>` is the problem here. – Mofi Oct 25 '21 at 16:31
  • @Mofi how would one go about doing it in plain python that seems like the most logical option here instead of using cmd – user72261 Oct 26 '21 at 12:37
  • Read in Python documentation chapter [7.2. Reading and Writing Files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files). You open the target file for write. Then open the first file for read, read in a loop the data in blocks and write them into the target file. Then close the completely read first file, open the second file for read, read the data in a loop in blocks of 64 KB or larger blocks and write the data into the target file, finally you close second input file and the target file. – Mofi Oct 26 '21 at 16:25
  • A very short version for a single file copy is posted in [this answer](https://stackoverflow.com/a/55309529/3074564). A single line solution cannot be used in your case as __two__ files must be copied to a target file. Another solution for a single file is in [this answer](https://stackoverflow.com/a/55851299/3074564) which can be easily extended for a second source file. See also [Python, how to read bytes from file and save it?](https://stackoverflow.com/questions/6787233/) Read also Python documentation [io - Core tools for working with streams](https://docs.python.org/3/library/io.html). – Mofi Oct 26 '21 at 16:33
  • In fact there are so many possible methods to read the binary byte streams from two files and write them into a third file that it is difficult to find out which one is with the best performance. See for example [Merge Two Binary Files Into Third Binary File](https://stackoverflow.com/questions/62050356/) working for small files which can be loaded completely into RAM. Well, I would use an approach which does not merge the bytes of the two source files first in RAM before writing them to the third file. – Mofi Oct 26 '21 at 16:40
  • There can be found on Stack Overflow with [\[python\] merge two files to third file](https://stackoverflow.com/search?q=%5Bpython%5D+merge+two+files+to+third+file) also other examples like [How to merge multiple files?](https://stackoverflow.com/questions/39312470/) – Mofi Oct 26 '21 at 16:44

0 Answers0