Algorithms <<
Previous Next >> Pybean
CPython
Based on Wikipedia: Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.
Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming. It is often described as a "batteries included" language due to its comprehensive standard library.
Guido van Rossum began working on Python in the late 1980s as a successor to the ABC programming language and first released it in 1991 as Python 0.9.0. Python 2.0 was released in 2000 and introduced new features such as list comprehensions, cycle-detecting garbage collection, reference counting, and Unicode support. Python 3.0, released in 2008, was a major revision that is not completely backward-compatible with earlier versions. Python 2 was discontinued with version 2.7.18 in 2020.
比對兩份資料, 列出差異:
數列應用
for 重複迴圈
以 utf-8 編碼讀出檔案內容
splitlines() 函式逐行將檔案內容放入數列
if 判斷式應用
# 兩份資料所在目錄字串
studlist_path = "Y:/studlist/2022spring/"
score_path = "Y:/score/2022/spring/"
# score 區檔案套稿字串
score_file_temp = "_stud_list.txt"
# 班級代號數列
classes = ["1a", "2a", "2b", "5j"]
# 利用 for 迴圈, 逐班進行處理
for i in classes:
# read data from studlist
# 以 utf-8 編碼開啟檔案
with open(studlist_path + i + ".txt", encoding="utf-8") as f:
studlist_data = f.read().splitlines()
#print(studlist_data)
with open(score_path + i + "/" + i + score_file_temp, encoding="utf-8") as f:
score_list_data = f.read().splitlines()
#print(score_list_data)
for i in studlist_data:
stud_num = i.split("\t")[0]
#print(stud_num)
# 列出已經不在評分名單中的學號
if stud_num not in score_list_data:
print(stud_num)
MS Teams assignment downloaded directory rename:
os 模組的 walk() 應用
next() 應用
讀取檔案內容
splitlines() 將各行資料納入數列
dictionary 資料結構應用
for 重複迴圈應用
try 與 except 應用
os 模組的 rename() 應用
import os
'''
MS Teams student submitted assignment files can be downloaded through SharePoint App, but directory named as student name,
we may use this program to rename directory into student number
'''
# from https://stackoverflow.com/questions/141291/how-to-list-only-top-level-directories-in-python
# rename directory: os.rename(source,destination)
stud_name_list = next(os.walk('./cd2022_student'))[1]
#print(stud_name_list)
# read data from wcm2022_5j_stud_name_email.txt
# firstname \t lastname \t email
with open("cd2022_stud_name_email.txt", encoding="utf-8") as f:
data = f.read().splitlines()
# set stud_name and stud_num into dict
stud_dict = {}
# skip the first line
for i in data[1:]:
line = i.split("\t")
firstname = line[0]
lastname = line[1]
# get student number from email
stud_num = line[2].split("@")[0]
stud_dict[lastname+firstname] = stud_num
print(stud_dict)
dir_pre = './cd2022_student/'
# loop stud_name_list and rename into stud_num
for i in stud_name_list:
try:
os.rename(dir_pre+i, dir_pre+stud_dict[i])
print("rename ", i, " into ", stud_dict[i])
except:
print(i, "no data")
Use old Python to create new portable Python:
gen_portable_python.py
import urllib.request
import os
# basic files for Python installation
py_list = ["core", "dev", "exe", "lib", "tcltk", "tools"]
# Python version
version = "3.10.6"
# location for Portable Python
path = "c:\\tmp\\Python310"
# Python msi download URL
ftp = "https://www.python.org/ftp/python/" + version + "/amd64/"
extract_path = path + "\\extract"
# create directory
try:
os.mkdir(path)
except:
# path exists
pass
# get Python installation msi files and extract into target dir
for i in py_list:
filename = i + ".msi"
url = ftp + filename
# download basic python msi file
urllib.request.urlretrieve(url, path+ "\\"+ filename)
os.system("msiexec.exe /a " + path + "\\" + i + ".msi /qb targetdir=" + extract_path)
# delete msi file
os.remove(extract_path + "\\" + i + ".msi")
search cmd to open command line window.
cd to c:\tmp where gen_portable_python.py located.
c:\tmp>c:\old_python_path\python gen_portable_python.py
Algorithms <<
Previous Next >> Pybean