forked from HaoZhang95/Python24
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic02.py
More file actions
54 lines (42 loc) · 1.26 KB
/
basic02.py
File metadata and controls
54 lines (42 loc) · 1.26 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
文件和文件夹的操作模块os
"""
import os
# 重命名
# os.rename("Test.txt","NewTest.txt")
# 删除
# os.remove("Test(备份).txt")
# 创建文件夹, open的方式是创建文件
if not os.path.exists("MyFolder"):
os.mkdir("MyFolder")
# 获取当前路径, getcwd()
myPath = os.getcwd()
print(myPath)
# 改变当前目录(类似于鼠标的返回上一级目录位置)
os.chdir("MyFolder")
print(os.getcwd())
# 无论是../或者是./都是相对路径
os.chdir("../")
print(os.getcwd())
# ../ 返回上一级目录, ./ 返回当前路径
os.chdir("./")
print(os.getcwd())
# 获取目录列表
result = os.listdir(os.getcwd())
print(result)
"""
批量修改文件名
1- 先创建模拟文件夹和10个文件
2- 开始批量的重命名
"""
folder_name = "MyNewFolder"
if not os.path.exists(folder_name):
os.mkdir("MyNewFolder")
# 创建文件之前先更改当前目录!
os.chdir(folder_name)
for i in range(1,11):
open("Test%d.txt" % i,"w").close()
file_list = os.listdir()
for temp in file_list:
new_name = temp.replace(".txt", "(新名字).txt") # temp识别不出是字符串,需要手动敲
os.rename(temp,new_name) # os的任何操作都是在根据当前目录下完成的,用前先设置当前目录