Advertisement
Wolvenspud

lab8

May 7th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import os
  2.  
  3. os.chdir('/students/u6431023/comp1730/lab8')
  4. fileObj = open("sample.txt")
  5.  
  6. def main():
  7.     #printLinesBackwards(fileObj)
  8.     print(filesInDir('/students/u6431023/comp1730/'))
  9.    
  10. def countLinesInFile(file):
  11.     posStart = file.tell()
  12.     numLines = sum(1 for line in file)
  13.     file.seek(posStart)
  14.     return numLines
  15.  
  16. def printLinesBackwards(file):
  17.     lines = []
  18.     num = countLinesInFile(file)
  19.     for i in range(num):
  20.         lines.append(file.readline())
  21.     for i in range(num-1, -1, -1):
  22.         print(lines[i])
  23.  
  24. def filesInDir(dirAddress):
  25.     if os.path.isdir(dirAddress):
  26.         os.chdir(dirAddress)
  27.         files = []
  28.         for obj in os.listdir():
  29.             print ('obj',obj)
  30.             if os.path.isfile(obj):
  31.                 files.append(obj)
  32.             elif os.path.isdir(obj):
  33.                 print ("why aren't ihere?")
  34.                 files.extend(filesInDir(obj))
  35.             else:
  36.                 print(obj)
  37.         return files
  38.  
  39.    
  40. if True:
  41.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement