Mosh的课程网址
Files Paths Demo 1: Basic Methods
1 2 3 4 5 6 7 8 9 10 from pathlib import Pathpath = Path(r"ecommerce/__init__.py" ) print(path.exists()) print(path.is_file()) print(path.is_dir()) print(path.name) print(path.stem) print(path.suffix) print(path.parent)
输出结果:
True True False __init__.py __init__ .py ecommerce
Demo 2
1 2 3 4 5 6 7 8 9 10 11 from pathlib import Pathpath = Path(r"ecommerce/__init__.py" ) path = path.with_name("file.txt" ) print(path) print(path.absolute()) path = path.with_suffix(".txt" ) print(path)
输出:
ecommerce\file.txt e:\Stone\Python\HelloWorld\ecommerce\file.txt ecommerce\file.txt
Working with Directories Demo 1: Basic Methods
1 2 3 4 5 6 7 from pathlib import Pathpath = Path("ecommerce" ) path.exists() path.mkdir() path.rmdir() path.rename("ecommerce2" )
Demo 2
1 2 3 4 from pathlib import Pathpath = Path("ecommerce" ) print(path.iterdir())
输出:
<generator object Path.iterdir at 0x00000180577926D0>
可见,得到的是一个generator object。因为目录下可能有大量文件,generator 更高效。
1 2 paths = [p for p in path.iterdir()] print(paths)
输出:
[WindowsPath(‘ecommerce/customer’), WindowsPath(‘ecommerce/shopping’), WindowsPath(‘ecommerce/__init__.py’), WindowsPath(‘ecommerce/__pycache__‘)]
1 2 3 paths = [p for p in path.iterdir() if p.is_dir()] print(paths)
输出:
[WindowsPath(‘ecommerce/customer’), WindowsPath(‘ecommerce/shopping’), WindowsPath(‘ecommerce/__pycache__‘)]
1 2 3 py_files = [p for p in path.glob("*.py" )] print(py_files)
输出:
[WindowsPath(‘ecommerce/__init__.py’)]
1 2 3 py_files = [p for p in path.rglob("*.py" )] print(py_files)
输出:
[WindowsPath(‘ecommerce/__init__.py’), WindowsPath(‘ecommerce/customer/contact.py’), WindowsPath(‘ecommerce/customer/__init__.py’), WindowsPath(‘ecommerce/shopping/sales.py’), WindowsPath(‘ecommerce/shopping/__init__.py’)]
Working with Files Demo 1
1 2 3 4 5 6 from pathlib import Pathpath = Path("ecommerce/__init__.py" ) path.exists() path.rename("init.txt" ) path.unlink()
Demo 2: 查看文件状态
1 2 3 from pathlib import Pathprint(path.stat())
输出:
os.stat_result(st_mode=33206, st_ino=1688849860483389, st_dev=971387, st_nlink=1, st_uid=0, st_gid=0, st_size=32, st_atime=1620876102, st_mtime=1620816590, st_ctime=1620814908)
将时间转为容易识别的形式:
1 2 3 from time import ctimeprint(ctime(path.stat().st_ctime))
输出:
Wed May 12 18:21:48 2021
Demo 3
1 2 3 4 print(path.read_bytes()) print(path.read_text()) path.write_bytes("..." ) path.write_text("..." )
输出:
b’print(“Ecommerce initialized”)\r\n’ print(“Ecommerce initialized”)
Demo 4:拷贝文件
1 2 3 4 5 6 7 8 from pathlib import Pathimport shutilsource = Path("ecommerce/__init__.py" ) target = Path() / "__init__.py" shutil.copy(source, target)
Working with Zip Files Demo 1
1 2 3 4 5 6 7 from pathlib import Pathfrom zipfile import ZipFilezip = ZipFile("files.zip" , "w" ) for path in Path("ecommerce" ).rglob("*.*" ): zip.write(path) zip.close()
Simpler way
1 2 3 4 5 6 from pathlib import Pathfrom zipfile import ZipFilewith ZipFile("files.zip" , "w" ) as zip: for path in Path("ecommerce" ).rglob("*.*" ): zip.write(path)
不需要手动close释放。
Demo 2
1 2 3 4 5 6 7 8 9 from pathlib import Pathfrom zipfile import ZipFilewith ZipFile("files.zip" ) as zip: info = zip.getinfo("ecommerce/__init__.py" ) print(info.file_size) print(info.compress_size) zip.extractall("extract" )
Working with CSV Files Demo 1: 写csv
1 2 3 4 5 6 7 import csvwith open("data.csv" , "w" , newline='' ) as file: writer = csv.writer(file) writer.writerow(["transaction_id" , "product_id" , "price" ]) writer.writerow([1000 , 1 , 5 ]) writer.writerow([1001 , 2 , 15 ])
如果不加newline=’’,文件会多空行
Demo 2: 读csv
1 2 3 4 5 import csvwith open("data.csv" ) as file: reader = csv.reader(file) print(list(reader))
输出:
[[‘transaction_id’, ‘product_id’, ‘price’], [‘1000’, ‘1’, ‘5’], [‘1001’, ‘2’, ‘15’]]
1 2 3 4 5 6 import csvwith open("data.csv" ) as file: reader = csv.reader(file) for row in reader: print(row)
输出:
[‘transaction_id’, ‘product_id’, ‘price’] [‘1000’, ‘1’, ‘5’] [‘1001’, ‘2’, ‘15’]
Working with JSON Files Demo 1: write JSON files
1 2 3 4 5 6 7 8 9 10 import jsonfrom pathlib import Pathmovies = [ {"id" : 1 , "title" : "Terminator" , "year" : 1989 }, {"id" : 2 , "title" : "Kindergarten Cop" , "year" : 1993 }, ] data = json.dumps(movies) Path("movies.json" ).write_text(data)
Demo 2: read JSON Files
1 2 3 4 5 6 import jsonfrom pathlib import Pathdata = Path("movies.json" ).read_text() movies = json.loads(data) print(movies[0 ])
输出:
{‘id’: 1, ‘title’: ‘Terminator’, ‘year’: 1989}
Working with a SQLite Database Demo 1: write
1 2 3 4 5 6 7 8 9 10 11 import sqlite3import jsonfrom pathlib import Pathmovies = json.loads(Path("movies.json" ).read_text()) with sqlite3.connect("db.sqlite3" ) as conn: command = "INSERT INTO Movies VALUES(?, ?, ?)" for movie in movies: conn.execute(command, tuple(movie.values())) conn.commit()
Demo 2: read
1 2 3 4 5 6 7 import sqlite3with sqlite3.connect("db.sqlite3" ) as conn: command = "SELECT * FROM Movies" cursor = conn.execute(command) for row in cursor: print(row)
或:
1 2 3 4 5 6 7 import sqlite3with sqlite3.connect("db.sqlite3" ) as conn: command = "SELECT * FROM Movies" cursor = conn.execute(command) movies = cursor.fetchall() print(movies)
Time Working with Timestamps 1 2 3 4 5 6 7 8 9 10 11 12 13 import timedef send_emails () : for i in range(1000000 ): pass start = time.time() send_emails() end = time.time() duration = end - start print(duration)
输出:
0.016989707946777344
Working with DateTimes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from datetime import datetimeimport timedt1 = datetime(2018 , 1 , 1 ) print(dt1) dt2 = datetime.now() print(dt2) print(dt2 > dt1) dt = datetime.strptime("2018/01/01" , "%Y/%m/%d" ) print(dt) dt = datetime.fromtimestamp(time.time()) print(f"{dt.year} /{dt.month} " ) print(dt.strftime("%Y/%m" ))
Working with Time Deltas 1 2 3 4 5 6 7 8 9 10 11 from datetime import datetime, timedeltadt1 = datetime(2020 , 1 , 1 ) + timedelta(days=1 , seconds=1000 ) print(dt1) dt2 = datetime.now() duration = dt2 - dt1 print(duration) print("days" , duration.days) print("seconds" , duration.seconds) print("total_seconds" , duration.total_seconds())
输出:
2020-01-02 00:16:40 497 days, 16:44:06.578196 days 497 seconds 60246 total_seconds 43001046.578196
Random Values Generating Random Values Demo 1
1 2 3 4 5 6 7 8 import randomimport stringprint(random.random()) print(random.randint(1 , 10 )) print(random.choice([1 , 2 , 3 , 4 ])) print(random.choices([1 , 2 , 3 , 4 ], k=4 )) print("" .join(random.choices("abcdefg" , k=4 )))
输出:
0.9791978038762406 2 3 [4, 1, 3, 3] eefe
Demo 2
1 2 3 4 5 6 import randomimport stringprint(string.ascii_letters) print(string.digits) print("" .join(random.choices(string.ascii_letters + string.digits, k=4 )))
输出:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 g02V
Demo 3:打乱顺序
1 2 3 4 5 import randomnumbers = [1 , 2 , 3 , 4 ] random.shuffle(numbers) print(numbers)
输出:
[4, 2, 3, 1]
Browser Opening the Browser 1 2 3 4 import webbrowserprint("Deployment completed" ) webbrowser.open("https://baidu.com" )
Sending Emails 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom pathlib import Pathimport smtplibmessage = MIMEMultipart() message["from" ] = "name_of_sender" message["to" ] = "email_of_recipients@email.com" message["subject" ] = "This is a test" message.attach(MIMEText("Body" )) message.attach(MIMEImage(Path("ww.jpg" ).read_bytes())) with smtplib.SMTP(host="smtp.whu.edu.cn" , port=25 ) as smtp: smtp.ehlo() smtp.starttls() smtp.login("email_of_sender@email.com" , "password" ) smtp.send_message(message) print("Sent..." )
Working with Templates 新建一个html文件 template.html,输入!回车会自动生成模板,删除head块中的内容,body块写入内容。最后如下:
1 2 3 4 5 6 7 8 9 10 11 <!DOCTYPE html> <html lang ="en" > <head > </head > <body > Hi <strong > $name</strong > , this is our test email. </body > </html >
strong表示字体加粗。
在上述email例子中:
1 2 3 from string import Templatetemplate = Template(Path("template.html" ).read_text())
1 2 3 body = template.substitute({"name" : "John" }) message.attach(MIMEText(body, "html" ))
即可。
Command-line Arguments Demo 1
1 2 3 import sysprint(sys.argv)
>python app.py -a -b -c
[‘app.py’, ‘-a’, ‘-b’, ‘-c’]
Demo 2
1 2 3 4 5 6 7 import sysif len(sys.argv) == 1 : print("USAGE: python3 app.py <password>" ) else : password = sys.argv[1 ] print("password" , password)
> python app.py
USAGE: python3 app.py <password>
> python app.py 1234
password 1234
Running External Programs Python执行命令 在 window 10 上跑的。
用python 执行windows命令dir
1 2 3 4 5 6 7 import subprocesscompleted = subprocess.run(["dir" , "." ], shell=True , capture_output=True , text=True ) print(type(completed))
capture_output=True使得输出在completed.stdout。
输出:
<class ‘subprocess.CompletedProcess’>
1 2 3 4 print("args" , completed.args) print("returncode" , completed.returncode) print("stderr" , completed.stderr) print("stdout" , completed.stdout)
输出:
> python app.py
args [‘dir’, ‘.’] returncode 0 stderr stdout 驱动器 E 中的卷没有标签。 卷的序列号是 000E-D27B
E:\Stone\Python\HelloWorld 的目录
2021/05/13 19:24 <DIR> . 2021/05/13 19:24 <DIR> .. 2021/05/06 16:44 <DIR> .vscode 2021/05/13 19:24 341 app.py 2021/05/06 16:15 8 content.txt 2021/05/13 15:36 54 data.csv 2021/05/13 15:55 0 db.sqlite3 2021/05/12 18:49 <DIR> ecommerce 2021/05/13 15:27 <DIR> extract 2021/05/13 14:42 3,647 files.zip 2021/05/13 15:48 102 movies.json 2021/05/13 18:03 138 template.html 2021/05/13 17:42 78,556 ww.jpg 2021/05/12 18:12 <DIR> __pycache__ 8 个文件 82,846 字节 6 个目录 46,196,027,392 可用字节
Python执行另一个python脚本 1 2 print("Here is a complicated script." )
1 2 3 4 5 6 7 8 9 10 11 import subprocesscompleted = subprocess.run(["python" , "other.py" ], shell=True , capture_output=True , text=True ) print("args" , completed.args) print("returncode" , completed.returncode) print("stderr" , completed.stderr) print("stdout" , completed.stdout)
> python app.py
args [‘python’, ‘other.py’] returncode 0 stderr stdout Here is a complicated script.
看到 stdout 就是 other.py 的输出。
错误命令 1 2 3 4 5 6 7 8 9 10 import subprocesscompleted = subprocess.run(["False" ], shell=True , capture_output=True , text=True ) print("args" , completed.args) print("returncode" , completed.returncode) print("stderr" , completed.stderr) print("stdout" , completed.stdout)
> python app.py
args [‘False’] returncode 1 stderr ‘False’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
stdout
1 2 if completed.returncode != 0 : print(completed.stderr)
也可以这样处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import subprocesstry : completed = subprocess.run(["False" ], shell=True , capture_output=True , text=True , check=True ) print("args" , completed.args) print("returncode" , completed.returncode) print("stderr" , completed.stderr) print("stdout" , completed.stdout) except subprocess.CalledProcessError as ex: print(ex)
输出:
Command ‘[‘False’]’ returned non-zero exit status 1.