python文件操作
#11111111111111111111111111
import os
from os.path import join,getsize,splitext
from os import listdir,remove,rename
os.chdir('D:\python_file')
#print(os.getcwd())
tt= ('html')
nn = ('.htm')
def operation(dirr):
global tt
global nn
for file in listdir(dirr):
temp = join(dirr,file)
if splitext(temp)[1] in tt:
ll,tt = splitext(temp)
rename(temp,ll+nn)
# print(1)
# os.remove(temp)
# print(temp,'deleted')
#print(os.getcwd())
operation(os.getcwd())
222222222222222222222222222
import os
from os.path import join,getsize,splitext
from os import listdir,remove,rename
old = os.getcwd()
print(os.getcwd())
os.chdir('C:\\')
print(os.getcwd())
os.chdir(old)
print(os.getcwd())
#33333333333333333333333333
import os
from os.path import join,getsize,splitext,isdir
from os import listdir,remove,renamejkjjjjj
father = input("目录:")
child = input("文件名:")
os.chdir(father)
print(os.getcwd())
t = 0
def fac(ty):
global father
global child
global t
for tt in listdir(father):
ttt = join(ty,tt)
print(ttt)
if isdir(tt):
fac(tt)
elif tt == child:
t = 1
fac(os.getcwd())
if t == 1:
print("存在")
else:
print("不存在")
#44444444444444444444444
import os
import os.path
from os import listdir,remove
from os.path import join,getsize,splitext,isdir
father = input("请输入地址:")
os.chdir(father)
file_type=('tem','log','obj','txt')
#for i in listdir(father):
# print(i)
def fac(ty):
global file_type
if not isdir(ty):
return ;
for filename in listdir(ty):
temp = join(ty,filename)
# print(temp)
if isdir(temp):
fac(temp)
#
elif splitext(temp)[1] in file_type or getsize(temp)==0:
remove(temp)
print(temp,'remove')
print(os.getcwd())
fac(father)
#55555555555555555555555555555
import os
import os.path
from os import listdir,remove
from os.path import join,getsize,splitext,isdir
father = input("请输入地址:")
os.chdir(father)
ft = ('.ppt')
fn = ('powerpoint2007')
n=0
def fac(ty):
global ft
global fn
global n
if not isdir(ty):
return ;
for filename in listdir(ty):
temp,temptype = splitext(filename)
ff = join(ty,filename)
if isdir(ff):
fac(ff)
elif temp == fn and temptype == ft:
n=n+1
fac(os.getcwd())
print(n)