Python 刪除檔案和檔名特定字元
阿新 • • 發佈:2019-01-29
#coding=utf-8
import os
import string
def re_file():
path = os.getcwd()
#filelist = os.listdir(path) #該資料夾下所有的檔案(包括資料夾)
for root, dirs,files in os.walk(path):
for name in files: #遍歷所有檔案
pathname = os.path.splitext(os.path.join(root,name))
if(pathname[1] != ".txt" and pathname[1] != ".png" and pathname[1] != ".exe"and pathname[1] != ".bin"): #刪除不是.txt的檔案
os.remove(os.path.join(root,name))
def re_name():
path = os.getcwd()
# filelist = os.listdir(path) #該資料夾下所有的檔案(包括資料夾)
for root, dirs, files in os.walk(path):
for name in files: # 遍歷所有檔案
pos = name.find(".tab.out") # 把檔案帶有.tab.out的字元刪除
if (pos == -1):
continue
newname = name[0:pos] + name[pos + 8:]
os.rename(os.path.join(root, name), os.path.join(root, newname))
re_file()
re_name()
import os
import string
def re_file():
path = os.getcwd()
#filelist = os.listdir(path) #該資料夾下所有的檔案(包括資料夾)
for root, dirs,files in os.walk(path):
for name in files: #遍歷所有檔案
pathname = os.path.splitext(os.path.join(root,name))
if(pathname[1] != ".txt" and pathname[1] != ".png" and pathname[1] != ".exe"and pathname[1] != ".bin"): #刪除不是.txt的檔案
os.remove(os.path.join(root,name))
def re_name():
path = os.getcwd()
# filelist = os.listdir(path) #該資料夾下所有的檔案(包括資料夾)
for root, dirs, files in os.walk(path):
for name in files: # 遍歷所有檔案
pos = name.find(".tab.out") # 把檔案帶有.tab.out的字元刪除
if (pos == -1):
continue
newname = name[0:pos] + name[pos + 8:]
os.rename(os.path.join(root, name), os.path.join(root, newname))
re_file()
re_name()