当前位置:首页 » 文件管理 » python遍历指定文件夹

python遍历指定文件夹

发布时间: 2022-09-03 03:28:13

A. python 递归遍历文件夹

没有仔细看,但你的第一句就有错
def distinguish_file(user_paht):
参数应为user_path

B. python获取文件夹下所有文件名

使用os模块可以获取指定文件夹下所有文件名,有两个方法os.walk()和os.listdir().
(1)os.walk可以用于遍历指定文件下所有的子目录、非目录子文件。
(2)os.listdir()用于返回指定的文件夹下包含的文件或文件夹名字的列表,这个列表按字母顺序排序。

C. python 遍历文件夹PDF并转换成图片

这种图片的也可以转换成图片的形式,一般文件夹要进行一个转化才可以变成图片。

D. 使用python遍历指定文件,并将文件夹中所有的网址作为主页下载这一页中的内容

我觉得我没明白你的问题,你是遍历文件,哪来的文件夹中的网址?

你是想把文件中的所有网址都下载到本地?
你可以搜一下“python urllib.urlretrieve”,应该能找到你想要的代码

E. python如何遍历当前目录下所有文件,并输出后缀不为 py的文件名称

#coding=utf-8
'''
Created on 2014-11-14

@author: Neo
'''
import os

def GetFileList(dir, fileList):
newDir = dir
if os.path.isfile(dir):
fileList.append(dir.decode('gbk'))
elif os.path.isdir(dir):
for s in os.listdir(dir):
#如果需要忽略某些文件夹,使用以下代码
#if s == "xxx":
#continue
newDir=os.path.join(dir,s)
GetFileList(newDir, fileList)
return fileList

list = GetFileList('D:\\workspace\\PyDemo\\fas', [])
for e in list:
print e

F. python如何遍历文件夹然后生成md5

importos,hashlib

defgetlistdir(path):
try:#如果path是一个文件的完整名称,os.listdir会抛出错误
fl=os.listdir(path)
exceptExceptionase:
fl=[]
finally:
returnfl

defgetallfile(path):
allfile=[]
fl=getlistdir(path)
iflen(fl)!=0:
fl=list(map(lambdax:path+'\'+x,fl))
allfile=allfile+fl
forfinfl:
allfile=allfile+getallfile(f)
returnallfile

defmakemd5(stri):
md5=hashlib.md5()
md5.update(stri.encode('utf-8'))
returnmd5.hexdigest()

defmain():
myfilelist=getallfile('.')#获取当前文件'.'中的所有文件和文件夹名list
myfilestr='|'.join(myfilelist)#文件list转换为以'|'分隔的字符串
print(myfilestr)#显示要进行md5摘要加密的字符
print("md5=",makemd5(myfilestr))#计算并显示md5码

main()

G. 求教python怎么遍历指定文件

监控目录 -- 分两个部分: 1. 扫描目录文件, 保持当前状态数据; 2. 状态数据的比较



importos
importfnmatch


defgetfileinfo(filename):
(mode,ino,dev,nlink,
uid,gid,size,atime,mtime,ctime)=os.stat(filename)
returndict(
modifytime=mtime,
createtime=ctime,
size=size,
)


classDirectoryMonitor(object):

def__init__(self,path,fnexp="*.*"):
self.path=path
self.fnexp=fnexp
self.files={}
self.scan()

defscan(self):
currentfiles={}
forpath,dirs,filesinos.walk(self.path):
forfinfnmatch.filter(files,self.fnexp):
fullname=os.path.join(path,f)
currentfiles[fullname]=getfileinfo(fullname)
lastfiles=self.files
self.files=currentfiles
returnself.check(lastfiles,currentfiles)

@staticmethod
defcheck(lastfiles,currfiles):
monitor={}
newer={}
forfinset(currfiles)-set(lastfiles):
newer[f]=currfiles[f]
ifnewer:
monitor["newer"]=newer
deleted={}
forfinset(lastfiles)-set(currfiles):
deleted[f]=lastfiles[f]
ifdeleted:
monitor["deleted"]=deleted
changed={}
forfinset(lastfiles)&set(currfiles):
iflastfiles[f]!=currfiles[f]:
changed[f]=currfiles[f]
ifchanged:
monitor["changed"]=changed
returnmonitor


deftester():
importtime
dm=DirectoryMonitor(r"/home/tim/data","*.txt")
time.sleep(20)
m=dm.scan()
ifm:
printm


if__name__=="__main__":
tester()

H. python如何实现for循环操作文件

python用for循环遍历文件操作,代码如下:

#!ursinenvpython
#encoding:utf-8#设置编码方式
importos
importre
classloop_file:
def__init__(self,root_dir,short_exclude=[],long_exclude=[],file_extend=[]):
self.root_dir=root_dir
self.short_exclude=short_exclude
self.long_exclude=long_exclude
self.file_extend=file_extend
def__del__(self):
pass
defstart(self,func):
self.func=func
returnself.loop_file(self.root_dir)
defloop_file(self,root_dir):
t_sum=[]
sub_gen=os.listdir(root_dir)
forsubinsub_gen:
is_exclude=False
forextendsinself.short_exclude:##在不检查文件、目录范围中
ifextendsinsub:##包含特定内容
is_exclude=True
break
ifre.search(extends,sub):##匹配指定正则
is_exclude=True
break
ifis_exclude:
continue
abs_path=os.path.join(root_dir,sub)
is_exclude=False
forexcludeinself.long_exclude:
ifexclude==abs_path[-len(exclude):]:
is_exclude=True
break
ifis_exclude:
continue
ifos.path.isdir(abs_path):
t_sum.extend(self.loop_file(abs_path))
elifos.path.isfile(abs_path):
ifnot"."+abs_path.rsplit(".",1)[1]inself.file_extend:##不在后缀名检查范围中
continue
t_sum.append(self.func(abs_path))
returnt_sum
if'__main__'==__name__:
root_dir=r'D:harness ewshoppingcart estcasepromosingle_promo'
short_exclude=['.svn','.*_new.rb']###不包含检查的短目录、文件
long_exclude=[]###不包含检查的长目录、文件
file_extend=['.rb']###包含检查的文件类型
lf=loop_file(root_dir,short_exclude,long_exclude,file_extend)
forfinlf.start(lambdaf:f):
printf

I. 求通过python实现,在指定目录下遍历所有文件,将以.txt为后缀的文件移动到另一指定目录中

target_dir = 'home/' #假定要拷贝到home目录
x = [ item for item in os.walk('.') ] #os.walk递归地遍历所有子文件夹
#返回的是一个list,list中每一个元素由3个部分:(path, dirs, files)
for path, dirs, files in x:
for file in files:
if file.endswith('.txt'): #找到以txt结尾的,之
shutil.( path+os.sep+file , target_dir )

热点内容
我的世界宝可梦服务器电脑和手机可以联机 发布:2024-10-12 13:11:59 浏览:781
编程折半法 发布:2024-10-12 13:11:59 浏览:753
c语言strlen函数 发布:2024-10-12 13:07:31 浏览:570
客户端数据存储复习资料 发布:2024-10-12 12:57:38 浏览:908
python字符串下标 发布:2024-10-12 12:52:53 浏览:623
如何更改设置路由器用户和密码 发布:2024-10-12 12:41:08 浏览:438
什么手机的配置高 发布:2024-10-12 12:36:24 浏览:958
fc加密数字资产 发布:2024-10-12 12:19:12 浏览:462
大王卡可以缓存视频吗 发布:2024-10-12 12:14:52 浏览:660
压缩临时文件 发布:2024-10-12 12:14:04 浏览:62