Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. ReplaceFile = windll.kernel32.ReplaceFileW
  2. ReplaceFile.restype = BOOL
  3. ReplaceFile.argtypes = [
  4. LPWSTR,
  5. LPWSTR,
  6. LPWSTR,
  7. DWORD,
  8. LPVOID,
  9. LPVOID,
  10. ]
  11.  
  12. REPLACEFILE_WRITE_THROUGH = 0x1
  13. REPLACEFILE_IGNORE_MERGE_ERRORS = 0x2
  14. REPLACEFILE_IGNORE_ACL_ERRORS = 0x4
  15.  
  16. from jaraco.windows.api.filesystem import ReplaceFile
  17. import os
  18.  
  19. open('orig-file', 'w').write('some content')
  20. open('replacing-file', 'w').write('new content')
  21. ReplaceFile('orig-file', 'replacing-file', 'orig-backup', 0, 0, 0)
  22. assert open('orig-file').read() == 'new content'
  23. assert open('orig-backup').read() == 'some content'
  24. assert not os.path.exists('replacing-file')
  25.  
  26. import fileinput
  27. for line in fileinput.input(filename,inplace=True, backup='.bak'):
  28. # inplace=True causes the original file to be moved to a backup
  29. # standard output is redirected to the original file.
  30. # backup='.bak' specifies the extension for the backup file.
  31.  
  32. # manipulate line
  33. newline=process(line)
  34. print(newline)
  35.  
  36. newcontents=process(contents)
  37. for line in fileinput.input(filename,inplace=True, backup='.bak'):
  38. print(newcontents)
  39. break
  40.  
  41. from boltons.fileutils import atomic_save
  42.  
  43. with atomic_save('/path/to/file.txt') as f:
  44. f.write('this will only overwrite if it succeeds!n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement