首页 技术 正文
技术 2022年11月18日
0 收藏 995 点赞 4,130 浏览 2079 个字

python之os库

os.name 判断现在正在实用的平台,Windows 返回 ‘nt‘; Linux 返回’posix‘

>>> os.name
'nt'

os.getcwd() 得到当前工作的目录。

>>> os.getcwd()
'D:\\Program Files\\Sublime Text 3'

os.listdir() 指定所有目录下所有的文件和目录名。例:

>>> os.listdir()
['5ae74167-5539-4ef6-93c6-dd0cf223b5ce.dmp', 'changelog.txt', 'crash_reporter.exe', 'msvcr100.dll', 'Packages', 'plugin_host.exe', 'python3.3.zip', 'python33.dll', 'subl.exe', 'sublime.py', 'sublime_plugin.py', 'sublime_text.exe', 'unins000.dat', 'unins000.exe', 'unins000.msg', 'update_installer.exe']

以列表的形式全部列举出来,其中没有区分目录和文件。

os.mkdir() 创建目录

>>> os.mkdir('新建文件夹')
>>>> os.listdir()
['5ae74167-5539-4ef6-93c6-dd0cf223b5ce.dmp', 'changelog.txt', 'crash_reporter.exe', 'msvcr100.dll', 'Packages', 'plugin_host.exe', 'python3.3.zip', 'python33.dll', 'subl.exe', 'sublime.py', 'sublime_plugin.py', 'sublime_text.exe', 'unins000.dat', 'unins000.exe', 'unins000.msg', 'update_installer.exe', '新建文件夹']

注意:这样只能建立一层,要想递归建立可用:os.makedirs()

os.rmdir() 删除指定目录

>>> os.listdir()
['5ae74167-5539-4ef6-93c6-dd0cf223b5ce.dmp', 'changelog.txt', 'crash_reporter.exe', 'msvcr100.dll', 'Packages', 'plugin_host.exe', 'python3.3.zip', 'python33.dll', 'subl.exe', 'sublime.py', 'sublime_plugin.py', 'sublime_text.exe', 'unins000.dat', 'unins000.exe', 'unins000.msg', 'update_installer.exe']

os.remove() 删除指定文件


os.path.isfile() 判断指定对象是否为文件。是返回True,否则False

>>> os.path.isfile('sublime.py')
True

os.path.isdir() 判断指定对象是否为目录。是True,否则False。例:

>>> os.path.isdir('sublime.py')
False

os.path.exists() 检验指定的对象是否存在。是True,否则False.例:

>>> os.path.exists('新建文件夹')
False

os.path.split() 返回路径的目录和文件名。例:

>>> temp = os.path.split(os.getcwd())
>>> type(temp)
<class 'tuple'>
>>> temp[0]
'D:\\Program Files'
>>> temp[1]
'Sublime Text 3'

此处只是把前后两部分分开而已。就是找最后一个‘/‘。

os.system() 执行shell命令。

>>> os.system("echo 'hello world'")
'hello world'
0

os.chdir() 改变目录到指定目录

>>> os.chdir(temp[0])
>>> os.getcwd()
'D:\\Program Files'

os.path.join(path, name) 连接目录和文件名。例:

>>> os.path.join(temp[0],temp[1])
'D:\\Program Files\\Sublime Text 3'

os.path.getsize() 获得文件的大小,如果为目录,返回0

>>> os.path.getsize('D:\\Program Files\\Sublime Text 3')
4096

os.path.basename(path) 返回文件名

>>> os.path.basename('D:\\Program Files\\Sublime Text 3')
'Sublime Text 3'

os.path.abspath() 获得绝对路径。

os.path.dirname(path) 返回文件路径

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848