10000 Merge pull request #2 from neryuuk/chapter3 · neryuuk/learning-python@a5cd3e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5cd3e8

Browse files
authored
Merge pull request #2 from neryuuk/chapter3
Chapter3
2 parents b155ad0 + 57b522d commit a5cd3e8

11 files changed

+154
-156
lines changed

Ch3 - Files/challenge_files.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Create directory results
2+
# Create file results.txt within directory
3+
# Write into the created file
4+
# total byte count of all files
5+
# the listing of files from current directory
6+
7+
from os import listdir, mkdir, path
8+
9+
10+
def main():
11+
createDirectory("results")
12+
writeData(computeData("./"), "results/results.txt")
13+
14+
15+
def createDirectory(name):
16+
try:
17+
mkdir(name)
18+
except FileExistsError:
19+
print("Folder '{}' already exists".format(name))
20+
except Exception as e:
21+
print("Could not create directory: {}".format(e))
22+
exit(1)
23+
24+
25+
def writeData(data, filePath):
26+
with open(filePath, "w+") as results:
27+
if results.mode == "w+":
28+
results.write("Total bytecount: {}\n".format(data[0]))
29+
results.write("Files list:\n")
30+
results.write("--------------\n")
31+
results.write("{}\n".format("\n".join(data[1])))
32+
results.close()
33+
34+
35+
def computeData(dir):
36+
bytecount = 0
37+
files = []
38+
for item in listdir(dir):
39+
if path.isfile(item):
40+
bytecount += path.getsize(item)
41+
files.append(item)
42+
return bytecount, files
43+
44+
45+
if __name__ == "__main__":
46+
main()

Ch3 - Files/challenge_solution.py

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

Ch3 - Files/files_finished.py

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

Ch3 - Files/files_start.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@
44
#
55

66

7-
def main():
7+
def main():
88
# Open a file for writing and create it if it doesn't exist
9+
# myfile = open("textfile.txt", "w+")
910

10-
1111
# Open the file for appending text to the end
12-
12+
# myfile = open("textfile.txt", "a+")
1313

1414
# write some lines of data to the file
15+
# for i in range(10):
16+
# myfile.write("This is some new text\n")
1517

16-
1718
# close the file when done
19+
# myfile.close()
1820

19-
2021
# Open the file back up and read the contents
22+
myfile = open("textfile.txt", "r")
23+
# if myfile.mode == 'r':
24+
# contents = myfile.read()
25+
# print(contents)
26+
if myfile.mode == 'r':
27+
lines = myfile.readlines()
28+
for line in lines:
29+
print(line)
30+
2131

22-
2332
if __name__ == "__main__":
2433
main()

Ch3 - Files/newfile.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
This is some text
2+
This is some text
3+
This is some text
4+
This is some text
5+
This is some text
6+
This is some text
7+
This is some text
8+
This is some text
9+
This is some text
10+
This is some text
11+
This is some new text
12+
This is some new text
13+
This is some new text
14+
This is some new text
15+
This is some new text
16+
This is some new text
17+
This is some new text
18+
This is some new text
19+
This is some new text
20+
This is some new text

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()

Ch3 - Files/shell_finished.py

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

Ch3 - Files/shell_start.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,34 @@
44
#
55

66
import os
7+
import shutil
78
from os import path
9+
from shutil import make_archive
10+
from zipfile import ZipFile
11+
812

913
def main():
1014
# make a duplicate of an existing file
11-
if path.exists("textfile.txt"):
15+
if path.exists("textfile.txt.bak"):
1216
# get the path to the file in the current directory
13-
17+
src = path.realpath("textfile.txt.bak")
18+
1419
# let's make a backup copy by appending "bak" to the name
15-
20+
# dst = src + ".bak"
21+
# shutil.copy(src, dst)
22+
1623
# rename the original file
17-
24+
# os.rename(src, "newfile.txt")
25+
1826
# now put things into a ZIP archive
27+
# root_dir, _ = path.split(src)
28+
# make_archive("archive", "zip", root_dir)
1929

2030
# more fine-grained control over ZIP files
31+
with ZipFile("test_zip.zip", "w") as newzip:
32+
newzip.write("newfile.txt")
33+
newzip.write("textfile.txt.bak")
34+
2135

22-
2336
if __name__ == "__main__":
2437
main()

Ch3 - Files/textfile.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
This is some text
2+
This is some text
3+
This is some text
4+
This is some text
5+
This is some text
6+
This is some text
7+
This is some text
8+
This is some text
9+
This is some text
10+
This is some text
11+
This is some new text
12+
This is some new text
13+
This is some new text
14+
This is some new text
15+
This is some new text
16+
This is some new text
17+
This is some new text
18+
This is some new text
19+
This is some new text
20+
This is some new text

0 commit comments

Comments
 (0)
0