导出文件路径列表通过批处理处理路径
Rename file:
def trimandrename(input):
inpath = Path(input)
if inpath.is_file():
newname = inpath.name
strarr = newname.split()
if len(strarr) > 1:
# newname = strarr[0] + strarr[1] + strarr[len(strarr)-1]
newname = strarr[0] + "_verse004.nii.gz"
inpath.rename(Path(inpath.parent, newname))
else:
for subdir in inpath.iterdir():
trimandrename(subdir)
output scripts:
from pathlib import Path
p = Path('./')
# All subdirectories in the current directory, not recursive.
file = open(r'', 'a')
[file.write(f.absolute().as_posix() + '\n') for f in p.iterdir() if f.is_dir()]
file.close()
windows patch scripts:
ECHO OFF
FOR /F %%i IN (myfile.txt) DO ECHO %%i
csv write file
def csv_writer(inpath, outpath, mode):
with open(outpath, mode, newline='') as file:
csv_file = csv.writer(file)
path = Path(inpath)
header = ['Num', 'Name']
csv_file.writerow(header)
num = 1
for subdir in path.iterdir():
if subdir.is_dir():
csv_file.writerow([num, subdir.name])
num = num + 1