-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_os_basic.py
More file actions
44 lines (34 loc) · 1.35 KB
/
python_os_basic.py
File metadata and controls
44 lines (34 loc) · 1.35 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
38
39
40
41
42
43
44
import os
from pathlib import Path
path = "F:/test/"
# for file in os.listdir(path):
# print(file)
# print('-----------------')
# print(os.path)
# print('-----------------')
# os.walk will recursively travers all files in all current and sub-directory
# root is the path of the directory that is being traversesd
# dir is the directory that are in the current directory that program is in
# files are all the files that are in the current directory
for root, dir, files in os.walk(path):
# print(f'{root} --> {dir} --> {files}\n')
for file in files:
if file.endswith('.png'):
print('------------------')
print(os.path.join(root, file))
# listing files and directory using scandir
path_2 = "/home/pankaj"
for items in os.scandir(path_2):
# st_mtime provides the time the content of the file was last modified
print(f'{items.name}--------------->{items.stat().st_mtime}')
# print(items.stat().st_mtime)
nl = '\n'
print(f'{nl}-----------------------------{nl}')
# listing files and directory using pathlib.Path
# pathlib.Path have an .itemdir() method for creating an iterator of all
# files and folders in a directory. each entry yielded by the .itemdir
# contains information about the file or directory such as name and other
# attributes.
entries = Path(path_2)
for entry in entries.iterdir():
print(entry.name)