walk_sys.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3. import os
  4. import os.path
  5. import hashlib
  6. def is_writeable(path, check_parent=False):
  7. '''
  8. Check if a given path is writeable by the current user.
  9. :param path: The path to check
  10. :param check_parent: If the path to check does not exist, check for the
  11. ability to write to the parent directory instead
  12. :returns: True or False
  13. '''
  14. if os.access(path, os.F_OK) and os.access(path, os.W_OK):
  15. # The path exists and is writeable
  16. return True
  17. if os.access(path, os.F_OK) and not os.access(path, os.W_OK):
  18. # The path exists and is not writeable
  19. return False
  20. # The path does not exists or is not writeable
  21. if check_parent is False:
  22. # We're not allowed to check the parent directory of the provided path
  23. return False
  24. # Lets get the parent directory of the provided path
  25. parent_dir = os.path.dirname(path)
  26. if not os.access(parent_dir, os.F_OK):
  27. # Parent directory does not exit
  28. return False
  29. # Finally, return if we're allowed to write in the parent directory of the
  30. # provided path
  31. return os.access(parent_dir, os.W_OK)
  32. def is_readable(path):
  33. '''
  34. Check if a given path is readable by the current user.
  35. :param path: The path to check
  36. :returns: True or False
  37. '''
  38. if os.access(path, os.F_OK) and os.access(path, os.R_OK):
  39. # The path exists and is readable
  40. return True
  41. # The path does not exist
  42. return False
  43. def findfile(start, name=None):
  44. for relpath, dirs, files in os.walk(start):
  45. if name in files:
  46. full_path = os.path.join(start, relpath, name)
  47. print(os.path.normpath(os.path.abspath(full_path)))
  48. def md5Checksum(filePath):
  49. fh = open(filePath, 'rb')
  50. m = hashlib.md5()
  51. while True:
  52. data = fh.read(8192)
  53. if not data:
  54. break
  55. m.update(data)
  56. fh.close()
  57. return m.hexdigest()
  58. # 还需要解决判断文件的类型,例如socket文件不能被计算,只能计算常规的文件
  59. def pathispath(ps_path):
  60. if os.path.isfile(ps_path):
  61. pa_path = os.path.split(ps_path)
  62. print(' '*32, pa_path[0])
  63. print(md5Checksum(ps_path))
  64. print(pa_path[1])
  65. else:
  66. if os.path.isdir(ps_path):
  67. for ps_one in os.walk(ps_path):
  68. print(' '*32, ps_one[0])
  69. for ps_file in ps_one[2]:
  70. if (os.path.isfile(os.path.join(ps_one[0], ps_file))):
  71. print(os.path.join(ps_one[0], ps_file))
  72. print(md5Checksum(os.path.join(ps_one[0], ps_file)))
  73. def printall(ps_path):
  74. if os.path.isfile(ps_path):
  75. pass
  76. def visit(arg, dirname, names):
  77. print(dirname, arg)
  78. for name in names:
  79. subname = os.path.join(dirname, name)
  80. if os.path.isdir(subname):
  81. print('%s/' % name)
  82. else:
  83. print('%s' % name)
  84. def main():
  85. #filepath = '/Users/light/'
  86. filepath = '/Users/light.zhang/'
  87. pathispath(filepath)
  88. if __name__ == '__main__':
  89. main()