python os.listdir()解决乱码
pythonos.listdir()解决乱码
计算机一般来说是需要定期的清理,系统的内存不能延伸,同时有一些不需要的文件也可以得以清除掉。有些人会使用os.remove来进行文件的清楚,从而导致一些错误的出现,可以说这是对于os.remove的用法还没有熟练掌握。下面我们就os.remove的详细用法及使用注意分别介绍。
1.语法
os.listdir(path)
2.参数
path–目标目录的路径,即路径的字符串表示。
3.返回值
指定路径下,文件和文件夹名字的列表。
4.实例
#!/usr/bin/python
#-*-coding:UTF-8-*-
importos,sys
#打开文件
path="/var/www/html/"
dirs=os.listdir(path)
#输出所有文件和文件夹
forfileindirs:
print(file)
如果目录下有中文目录,打印时遇到乱码解决方法:
cPath=os.getcwd()
#如果目录名字为中文需要转码处理
uPath=unicode(cPath,'utf-8')
forfileNameinos.listdir(uPath):
print(fileName)
以上就是pythonos.listdir()解决乱码的方法,可以说除了可以进行返回文件夹的用法,在处理乱码的问题上,os.listdir()的使用也是可以解决的。更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
pythonPexpect的工作流程
python中Pexpect的工作流程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、工作流程步骤(1)用spawn来执行一个程序;(2)用expect...详情>>
2023-11-08 23:50:45
python面向对象中类如何实现?
python面向对象中类如何实现?一、类的功能1、遵守变量命名的规范;2、大驼峰(有一个或者多个单词构成,每个单词首字母大写,单词跟单词直接相连)...详情>>
2023-11-08 22:49:52
python上下文管理器的用法详解
python上下文管理器的用法详解本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明上下文管理器是一个包装任意代码块的对象。上...详情>>
2023-11-08 21:06:42
python中list与array有什么区别?
python中list与array有什么区别?在python数据类型中,list和array都可以根据索引来取其中的元素,但是list可以用append或者+来新增元素或者添加...详情>>
2023-11-08 20:34:31热门推荐
pythonPexpect的工作流程
沸python继承是什么?
热python面向对象中类如何实现?
热python实现Excel数据的探索和清洗
新python制表符添加空白有哪些方法?
python中如何使用@contextmanage?
python上下文管理器的用法详解
python中list与array有什么区别?
python中time.clock()的使用
python中Array和DataFrame如何相互转换
python的ndarray与pandas的series如何相互转换?
python os.listdir()解决乱码
python中numpy数组合并的两种方法
python中string和float之间的转换
技术干货






