python實現根據檔案格式分類

2021-09-03 07:44:30 字數 1464 閱讀 1961

使用到python內建os模組(對目錄或檔案的新建/刪除/屬性檢視,還提供了對檔案以及目錄的路徑操作)shutil模組(高等級的目錄或檔案的移動/複製/打包/壓縮/解壓等操作)

import os,shutil,time

deffiles_classfy

(target_path)

:global count #定義全域性變數

file_list = os.listdir(target_path)

#列出目標路徑下的所有檔案列表

forfile

in file_list:

#遍歷取到每乙個檔名

os.chdir(target_path)

#改變當前工作目錄為目標路徑

iffile

.find(

'.')==-

1:#如果當前檔名中無副檔名則跳過

continue

filetype =

file

.split(

'.')[-

1]#取得副檔名格式,windows下檔案需設定為副檔名可見

ifnot os.path.exists(filetype)

: os.mkdir(filetype)

#如果工作目錄下不存在以當前副檔名命名的資料夾則建立該資料夾(預設屬性為0777)

new_path = os.path.join(target_path,

'%s'

%filetype)

#取得當前副檔名資料夾路徑

os.chdir(new_path)

if os.path.exists(

file):

#如果當前副檔名資料夾中已存在同名檔案則跳過

continue

else

: os.chdir(target_path)

#將工作目錄切換回目標資料夾

shutil.move(

file

,filetype)

#移動相同格式的檔案到對應的格式資料夾

count+=

1start = time.time(

)count =

0path =

"g:\picture\mi5splus"

files_classfy(path)

total_time = time.time(

)- start

print

(%total_time)

print

("共處理:%d"

%count)

執行結果:

Python檔案格式轉換

將帶有點座標的 csv 轉 geojson,不帶其他屬性 import csv csv file open e test.csv encoding gbk csv reader csv.reader csv file geojson properties type feature float row...

Oracle 控制檔案格式ctl檔案格式

options skip 1,errors 10 load data characterset zhs16gbk infile into table table name truncate fields terminated by optionally enclosed by trailing nu...

檔案格式 gff格式

gff檔案格式 gff格式是 sanger 研究所定義,是一種簡單的 方便的對於 dna rna以及蛋白質序列的特徵進行描述的一種資料格式,已經成為序列注釋的通用格式,比如基因組的基因 許多軟體都支援輸入或者輸出gff格式。前格式定義的最新版本是版本3。原始定義見 song websitegff是存...