forked from arvimal/100DaysofCode-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-scanning_dirs.py
More file actions
37 lines (32 loc) · 1.01 KB
/
05-scanning_dirs.py
File metadata and controls
37 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
# os.scandir() returns an iterator
# This iterator stands for a directory-entry for
# the folder passed to `os.scandir(<folder>).
# This iterator also gives methods which can be used
# to check if the entry is a file, folder, symlink etc.
# The iterator can be stat using <iterator>.stat
# From Py3MoTD:
"""
scandir() returns a sequence of DirEntry instances
for the items in the directory.
This object has several attributes and methods
for accessing metadata about the file.
"""
import os
import sys
if len(sys.argv) == 1:
path = "/tmp"
else:
path = sys.argv[1]
for i in os.scandir(path):
if i.is_dir():
data_type = "Directory"
elif i.is_file():
data_type = "File"
elif i.is_symlink():
data_type = "Symbolic link"
print("\n# `{}`".format(i.name))
print("\tType: {}".format(data_type))
print("\tInode #: {}".format(i.stat().st_ino))
print("\tModified time: {}".format(i.stat().st_mtime))
print("\tStating all info: \n{}".format(i.stat()))