cfabio

Files.py

Jan 6th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. import os
  2.  
  3. filePath = r'c:\spam'
  4.  
  5. #create path
  6. fullPath = os.path.join(filePath, 'folder', 'eggs.png')
  7. print(fullPath)
  8.  
  9. #get working directory
  10. os.getcwd()    
  11.  
  12. #change working directory
  13. os.chdir('c:\\drivers')
  14.  
  15.  
  16. #absolute/relative path
  17. os.path.abspath('..\\drivers')
  18. os.path.isabspath()
  19. os.path.relpath(fullpath, 'c\\spam') #relative path from a starting point
  20.  
  21. os.path.dirname(fullpath)
  22. os.path.basename(fullpath)
  23.  
  24. #exists
  25. os.path.exists(fullpath)
  26. os.path.isfile(fullpath)
  27. os.path.isfolder(fullpath)
  28.  
  29. os.path.getsize(fullpath)
  30.  
  31. os.listir('c:\\driver')
  32.  
  33. os.makedirs('c:\\driver2')
  34.  
  35.  
  36. ########################################
  37. #reading/writing files
  38. ########################################
  39. filePath = r'c:\spam\myFile.txt'
  40.  
  41. helloFile = open(filePath) #read mode
  42. content = helloFile.read() #to read the file twice I need to call again open
  43. helloFile.close()
  44.  
  45. lines = helloFile.readlines()
  46.  
  47. helloFile = open(filePath, 'w') #write mode
  48. helloFile.write('Bla') #it does not add the \n automatically
  49.  
  50.  
  51. #shelve files; allow to store list, dictionary and not-text data to
  52. #a binary file.
  53. import shelve
  54. shelFile = shelve.open('mydata')
  55. shelFile['cats'] = ['bla', 'ciao', 'miao']
  56. shelFile.close()
  57.  
  58. #can be read with:
  59. shelFile = shelve.open('mydata')
  60. print(shelFile['cats'])
  61.  
  62. #it works like a dictionary
  63. shelFile.keys()
  64. shelFile.values()
  65.  
  66. ########################################
  67. #copy/move
  68. ########################################
  69. import shutil
  70.  
  71. //copy single file
  72. shutil.copy(fileSrcPath, dirPath)
  73. shutil.copy(fileSrcPath, fileTargetPath) //copy and rename
  74.  
  75. //copy a directory
  76. shutil.copytree(SrcFolder, targetFolder)
  77.  
  78. shutil.move(fileSrcPath, dirPath)
  79. shutil.move(fileSrcPath, fileTargetPath) //rename
  80.  
  81. ########################################
  82. #deletion
  83. ########################################
  84. os.unlink(fileName)
  85. os.rmdir(dirName) #remove directory if empty
  86.  
  87. import shutil
  88. shutil.rmtree(dirName) #delete the folder and its content
  89.  
  90. import send2trash
  91. send2trash.send2trash(fileName) #send to trash instead of permanently removing
  92.  
  93. ########################################
  94. #walk a folder tree and allow to do operations in each level of the tree
  95. ########################################
  96. for folderName, subfolders, filenames in os.walk(folderName) :
  97.     print('Folder is' + folderName)
  98.     print('Subfolders of ' + folderName + ' are ' + str(subfolders))
  99.     print('Filenames of ' + folderName + ' are ' + str(filenames))
  100.  
  101.     for subfolder in subfolders :
  102.         #...do something
  103.  
  104.     for file in filenames :
  105.         #...do something
Advertisement
Add Comment
Please, Sign In to add comment