#coding=utf-8
import os
def GetFileList(dir, fileList):
newDir = dir
files = os.listdir(dir);
for file in files:
newDir = os.path.join(dir,file)
if os.path.isfile(newDir):
fileList.append(newDir)
elif os.path.isdir(newDir):
GetFileList(newDir,fileList)
return fileList
dirpath="/public/data/"
outpath="/public/home/sun/data/"
items = GetFileList(dirpath,[])
for item in items:
if item.endswith(".txt"):
name = item.split('/')[-1] #split列表最后一个元素
out_cmd = '命令行'
os.popen(out_cmd)
print "finished!"
import os
def GetFileList(dir, fileList):
newDir = dir
files = os.listdir(dir);
for file in files:
newDir = os.path.join(dir,file)
if os.path.isfile(newDir):
fileList.append(newDir)
elif os.path.isdir(newDir):
GetFileList(newDir,fileList)
return fileList
dirpath="/public/data/"
outpath="/public/home/sun/data/"
items = GetFileList(dirpath,[])
for item in items:
if item.endswith(".txt"):
name = item.split('/')[-1] #split列表最后一个元素
out_cmd = '命令行'
os.popen(out_cmd)
print "finished!"
本文介绍了一个Python脚本,该脚本能够递归地遍历指定目录下的所有.txt文件,并对每个文件执行命令行操作。具体步骤包括获取目录路径、递归遍历目录树、收集所有.txt文件的完整路径并执行特定命令。
826

被折叠的 条评论
为什么被折叠?



