Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ReplaceFile = windll.kernel32.ReplaceFileW
- ReplaceFile.restype = BOOL
- ReplaceFile.argtypes = [
- LPWSTR,
- LPWSTR,
- LPWSTR,
- DWORD,
- LPVOID,
- LPVOID,
- ]
- REPLACEFILE_WRITE_THROUGH = 0x1
- REPLACEFILE_IGNORE_MERGE_ERRORS = 0x2
- REPLACEFILE_IGNORE_ACL_ERRORS = 0x4
- from jaraco.windows.api.filesystem import ReplaceFile
- import os
- open('orig-file', 'w').write('some content')
- open('replacing-file', 'w').write('new content')
- ReplaceFile('orig-file', 'replacing-file', 'orig-backup', 0, 0, 0)
- assert open('orig-file').read() == 'new content'
- assert open('orig-backup').read() == 'some content'
- assert not os.path.exists('replacing-file')
- import fileinput
- for line in fileinput.input(filename,inplace=True, backup='.bak'):
- # inplace=True causes the original file to be moved to a backup
- # standard output is redirected to the original file.
- # backup='.bak' specifies the extension for the backup file.
- # manipulate line
- newline=process(line)
- print(newline)
- newcontents=process(contents)
- for line in fileinput.input(filename,inplace=True, backup='.bak'):
- print(newcontents)
- break
- from boltons.fileutils import atomic_save
- with atomic_save('/path/to/file.txt') as f:
- f.write('this will only overwrite if it succeeds!n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement