3 回答
TA貢獻1815條經驗 獲得超10個贊
PHP實現(思路):
<?php
chdir('/home/eechen/TEST');
$path = pathinfo('/home/eechen/TEST/AA_BB_CC_DD.txt');
$arr = explode('_',$path['filename']);
$count = count($arr);
foreach($arr as $k => $v) {
if($k!=$count-1) {
if(!file_exists($v)) {
mkdir($v);
chdir($v);
echo getcwd()."\n";
} else {
chdir($v);
}
} else {
$file = $v.'.'.$path['extension'];
if(!file_exists($file)) {
touch($file);
echo getcwd().'/'.$file."\n";
}
}
}TA貢獻1946條經驗 獲得超3個贊
import os
import shutil
dir="C:\\Users\\Lenovo\\Desktop\\figure-it\\學習\\python";
fileNames = os.listdir(dir);
if(len(fileNames)>0):
for fn in fileNames:
if(fn.find("_") != -1):
n = fn[0:len(fn)-4];
names = n.split('_');
last = names[-1];
suffix = fn[len(fn)-4:len(fn)];
path = "";
for name in names:
path = os.path.join(path,name);
fullpath = os.path.join(dir,path);
if not os.path.exists(fullpath):
os.makedirs(fullpath);
shutil.copy(os.path.join(dir,fn),fullpath);
os.remove(os.path.join(dir,fn));
os.rename(os.path.join(fullpath,fn),os.path.join(fullpath,last+suffix));
- 3 回答
- 0 關注
- 360 瀏覽
添加回答
舉報
