-
Notifications
You must be signed in to change notification settings - Fork 0
/
z_文件处理.py
62 lines (45 loc) · 1.38 KB
/
z_文件处理.py
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
55
56
57
58
59
60
61
62
#coding:utf-8
f = open("xxx.txt","r")
#读取文件
content = f.read()
print(content)
f.close()
old_file_name = input("请输入copy文件名:")
old_file = open(old_file_name,"r")
new_file = open("cpcp.py","w")
cont = old_file .read()
new_file.write(cont)
old_file.close()
new_file.close()
#1. 获取用户要复制的文件名
old_file_name = input("请输入要复制的文件名:")
#2. 打开要复制的文件
old_file = open(old_file_name,"r")
#test.py -----> test[复件].py
#new_file_name = "[复件]"+old_file_name
position = old_file_name.rfind(".")
new_file_name = old_file_name[:position] + "[复件]" + old_file_name[position:]
#3. 新建一个文件
#new_file = open("laowang.txt", "w")
new_file = open(new_file_name, "w")
#4. 从旧文件中读取数据,并且写入到新文件中
while True:
content = old_file.read(1024)
if len(content)==0:
break
new_file.write(content)
#5. 关闭2个文件
old_file.close()
new_file.close()
import os
#1. 获取要重命名的文件夹 名字
folder_name = input("请输入要重命名的文件夹:")
#2. 获取制定的文件夹中的所有 文件名字
file_names = os.listdir(folder_name)
#os.chdir(folder_name)
#3. 批量重命名
for name in file_names:
print(name)
old_file_name = folder_name+"/"+name
new_file_name = folder_name+"/"+"[京东出品]-"+name
os.rename(old_file_name, new_file_name)