感兴趣Python扫描目录读取
文件,不考虑子目录的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
# *** 网 (jb51.cc) ***
try:
source_dir = '/dir/source/'
target_dir = '/dir/old/'
for root,sub_dirs,files in os.walk(source_dir):
for special_file in files:
spcial_file_dir = os.path.join(root,special_file)
# 打开文件的两种方式
# 1.文件以绝对路径方式
with open(spcial_file_dir) as source_file:
# 2.文件以相对路径方式
# with open(r'dir_test/test.txt') as source_file:
for line in source_file:
# do something
# 移动文件
shutil.move(spcial_file_dir,target_dir)
logger.info(u'文件%s移动成功'% spcial_file_dir)
return HttpResponse(json.dumps({'result':0}))
except Exception as e:
# do something