-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathospathutils
36 lines (28 loc) · 1.05 KB
/
ospathutils
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
#
# Example file for working with os.path module
#
import os
from os import path
import datetime
from datetime import date, time, timedelta
import time
def main():
# print the name of the OS
print(os.name)
# check for item existence and type
print("Item exists: "+str(path.exists("textfile.txt")))
print("Item is a file: "+str(path.isfile("textfile.txt")))
print("Item is a directory: "+str(path.isdir("textfile.txt")))
# work with the paths
print("Item path: "+str(path.realpath("textfile.txt")))
print("Item path and name: "+str(path.split(path.realpath("textfile.txt"))))
# get the modification time
t = time.ctime(path.getmtime("textfile.txt"))
print(t)
print(datetime.datetime.fromtimestamp(path.getmtime("textfile.txt")))
# calculate how long ago the item as modified
td = datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime("textfile.txt"))
print("It has been " + str(td) + "since the file was modified")
print("Or, " + str(td.total_seconds()) + "seconds")
if __name__ == "__main__":
main()