Skip to content

Commit 457f166

Browse files
committed
2024/03/24 - Updating Demo python files
1 parent b4f7d28 commit 457f166

20 files changed

+108
-0
lines changed

advanced/PYthonDemo_02_OS_Module.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# This file provides demo of OS module
2+
3+
import os
4+
5+
file_path = ""
6+
7+
8+
def getDetails():
9+
# to get OS name
10+
print(f"OS name: {os.name}")
11+
12+
# to get CWD
13+
print(f"Current working dir: {os.getcwd()}")
14+
15+
16+
def createDir():
17+
global file_path
18+
file_path = os.getcwd() + "/os_demo/"
19+
is_dir_exist = os.access(file_path, os.F_OK)
20+
print("directory {} exist: {}".format(file_path, is_dir_exist))
21+
# to create new dir
22+
if not is_dir_exist:
23+
print(f"Creating dir {file_path}")
24+
os.mkdir(file_path)
25+
26+
27+
def deleteDir():
28+
global file_path
29+
print(f"Deleting dir {file_path}")
30+
os.rmdir(file_path)
31+
32+
33+
def read_or_write(operation_name):
34+
global file_path
35+
36+
if operation_name == "w":
37+
file = open(file_path + "test.txt", "w")
38+
print("File obj:", file)
39+
print("Writing info into file..")
40+
file.write("This is OS module demo!!")
41+
file.close()
42+
else:
43+
file = open(file_path + "test.txt", "r")
44+
print(file.__dict__)
45+
print("File obj:", file)
46+
print("Reading info from file..")
47+
info = file.read()
48+
print(info)
49+
file.close()
50+
51+
52+
def checkAccess():
53+
global file_path
54+
txt_file_path = file_path + "test.txt"
55+
print(f"Access to file path {txt_file_path}: {os.access(txt_file_path, os.F_OK)}")
56+
print(f"Access to read file {txt_file_path}: {os.access(txt_file_path, os.R_OK)}")
57+
print(f"Access to write into file {txt_file_path}: {os.access(txt_file_path, os.W_OK)}")
58+
print(f"Access to execute file {txt_file_path}: {os.access(txt_file_path, os.X_OK)}")
59+
60+
61+
def renameDir():
62+
global file_path
63+
# renaming existing dir
64+
print(f"Existing dir: {file_path}")
65+
new_dir = file_path.replace("os_demo", "os_rename_demo")
66+
print(f"New dir: {new_dir}")
67+
os.rename(file_path, new_dir)
68+
69+
70+
def main():
71+
getDetails()
72+
createDir()
73+
read_or_write("r")
74+
checkAccess()
75+
renameDir()
76+
77+
78+
if __name__ == "__main__":
79+
main()

advanced/PythonDemo_01_Math_Module.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file provides demo of math module
2+
import math
3+
4+
n1, n2 = 5, 144
5+
n3, n4, n5 = 5.02, 5.72, 5.50
6+
n6, n7, n8 = -0.13, 39, -23
7+
8+
print(f"Square of {n1}: {math.pow(n1, 2)}")
9+
print(f"Square root of {n2}: {math.sqrt(n2)}")
10+
print(f"Factorial of {n1}: {math.factorial(n1)}")
11+
12+
print(f"ceil of {n3}: {math.ceil(n3)}")
13+
print(f"ceil of {n4}: {math.ceil(n4)}")
14+
print(f"ceil of {n5}: {math.ceil(n5)}")
15+
print(f"ceil of {n6}: {math.ceil(n6)}")
16+
17+
18+
print(f"floor of {n3}: {math.floor(n3)}")
19+
print(f"floor of {n4}: {math.floor(n4)}")
20+
print(f"floor of {n5}: {math.floor(n5)}")
21+
print(f"floor of {n6}: {math.floor(n6)}")
22+
23+
24+
print(f"absolute value of {n6}: {math.fabs(n6)}")
25+
print(f"absolute value of {n7}: {math.fabs(n7)}")
26+
print(f"absolute value of {n8}: {math.fabs(n8)}")
27+
28+
print("value of pi: ", math.pi)

advanced/os_rename_demo/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is OS module demo!!
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)