8000 Working with OS path utilities · neryuuk/learning-python@2a78a38 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a78a38

Browse files
committed
Working with OS path utilities
1 parent 2e112d9 commit 2a78a38

File tree

2 files changed

+15
-42
lines changed

2 files changed

+15
-42
lines changed

Ch3 - Files/ospathutils_finished.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

Ch3 - Files/ospathutils_start.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,29 @@
1212

1313
def main():
1414
# Print the name of the OS
15+
print(os.name)
1516

16-
1717
# Check for item existence and type
18+
print("Item exists: {}".format(path.exists("textfile.txt")))
19+
print("Item is a file: {}".format(path.isfile("textfile.txt")))
20+
print("Item is a directory: {}".format(path.isdir("textfile.txt")))
1821

19-
2022
# Work with file paths
23+
print("Item's path is: {}".format(path.realpath("textfile.txt")))
24+
print("Item's path and name: {}".format(
25+
path.split(path.realpath("textfile.txt"))
26+
))
2127

22-
2328
# Get the modification time
29+
t = time.ctime(path.getmtime("textfile.txt"))
30+
print(t)
31+
print(datetime.datetime.fromtimestamp(path.getmtime("textfile.txt")))
2432

25-
2633
# Calculate how long ago the item was modified
34+
td = datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime("textfile.txt"))
35+
print("It has been {} since the file was modified".format(td))
36+
print("Or, {} seconds".format(td.total_seconds()))
37+
2738

28-
2939
if __name__ == "__main__":
3040
main()

0 commit comments

Comments
 (0)
0