Use os.fdopen instead otherwise you'll get: TypeError: coercing to Unicode: need string or buffer, int found. Open and read 1st file Open and read 2nd file Copy the values of 2nd file to 1st file with header Write new value to 1st file That is, 1st file is opened as read-write mode and 2nd file is as read mode. The built-in open() in Python 2.x doesn't support opening by file descriptor. With open('filepath', 'w', opener=opener) as fh: open will generate the appropriate flags for our opener according to the requested mode ( w): import os Using a custom opener will make things easier and less error-prone. # the descriptor is automatically closed when fh is closed # The default umask is 0o22 which turns off write permission of group and others The data being written will be inserted at the end, after the existing data.If you don't want to use os.chmod and prefer to have the file created with appropriate permissions, then you may use os.open to create the appropriate file descriptor and then open the descriptor: import os The handle is positioned at the end of the file. The file is created if it does not exist.
0 Comments
Leave a Reply. |