Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. [TOC]
  2.  
  3. # writing file
  4.  
  5. ## using pickle
  6. ```python
  7.  
  8. try:
  9. f = open(pickle_file, 'wb')
  10. save = {
  11. 'train_dataset': train_dataset,
  12. 'train_labels': train_labels,
  13. 'valid_dataset': valid_dataset,
  14. 'valid_labels': valid_labels,
  15. 'test_dataset': test_dataset,
  16. 'test_labels': test_labels,
  17. }
  18. pickle.dump(save, f, pickle.HIGHEST_PROTOCOL)
  19. f.close()
  20. except Exception as e:
  21. print('Unable to save data to', pickle_file, ':', e)
  22. raise
  23. ```
  24.  
  25.  
  26. ## write to file
  27. ```python
  28. f = open('myfile','w')
  29. f.write('hi there\n') # python will convert \n to os.linesep
  30. f.close() # you can omit in most cases as the destructor will call it
  31. ```
  32.  
  33.  
  34.  
  35.  
  36. ## file name
  37. ```python
  38. if not os.path.exists(filename):
  39. ```
  40.  
  41. ## get file info
  42. ```python
  43. statinfo = os.stat(filename)
  44. ```
  45.  
  46. ## concat directory + file name
  47. ```python
  48. image_file = os.path.join(folder, image)
  49. ```
  50.  
  51. ## list all files
  52. ```python
  53. files = os.listdir(folder)
  54. ```
  55.  
  56. ## list all directories
  57. ```python
  58. data_folders = [
  59. os.path.join(root, d) for d in sorted(os.listdir(root))
  60. if os.path.isdir(os.path.join(root, d))]
  61. ```
  62.  
  63. ## directory
  64. ```python
  65. if os.path.isdir(root)
  66. ```
  67.  
  68. ## file size differs
  69. ```python
  70. if statinfo.st_size == expected_bytes:
  71. print('Found and verified', filename)
  72. else:
  73. raise Exception(
  74. 'Failed to verify ' + filename + '. Can you get to it with a browser?')
  75. return filename
  76. ```
  77.  
  78.  
  79.  
  80. ## extract zip files
  81. ```python
  82. zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
  83. zip_ref.extractall(directory_to_extract_to) # only if destiation directory is different
  84. zip_ref.close()
  85. ```
  86.  
  87. ## extract tar files
  88. ```python
  89. tar = tarfile.open(filename)
  90. sys.stdout.flush()
  91. tar.extractall()
  92. tar.close()
  93. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement