详解python中的文件与目录操作

yipeiwu_com6年前Python基础

详解python中的文件与目录操作

一 获得当前路径

1、代码1

>>>import os
>>>print('Current directory is ',os.getcwd())
Current directory is D:\Python36

2、代码2

如果将上面的脚本写入到文件再运行

Current directory is E:\python\work

二 获得目录的内容

Python代码

>>> os.listdir (os.getcwd()) 
['DLLs','Doc','include','Lib','libs','LICENSE.txt','NEWS.txt','PyOpenGL-wininst.log','python.exe','
python3.dll','python36.dll','pythoncom36.dll','pythonw.exe','pywin32-wininst.log','pywintypes36.dll',
'RemovePyOpenGL.exe','Removepywin32.exe','Scripts','tcl','Tools','vcruntime140.dll'] 

 三 创建目录

>>>import os
>>> os.mkdir('temp')

 四 删除目录

>>> os.rmdir('temp')

五 判断是否是目录

>>> os.path.isdir('E:\\python')
True

六 判断是否是文件

>>> os.path.isfile('E:\\python\\work\\test.py')
True

 以上就是python中的文件与目录操作的简单操作,如有疑问可以参考本站的相关的资料,欢迎留言讨论!感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

Opencv+Python实现图像运动模糊和高斯模糊的示例

Opencv+Python实现图像运动模糊和高斯模糊的示例

运动模糊:由于相机和物体之间的相对运动造成的模糊,又称为动态模糊 Opencv+Python实现运动模糊,主要用到的函数是cv2.filter2D(): # coding: utf-...

对python中Matplotlib的坐标轴的坐标区间的设定实例讲解

对python中Matplotlib的坐标轴的坐标区间的设定实例讲解

如下所示: <span style="font-family: Arial, Helvetica, sans-serif;">>>> import nu...

详解Python读取配置文件模块ConfigParser

1,ConfigParser模块简介 假设有如下配置文件,需要在Pyhton程序中读取 $ cat config.ini [db] db_port = 3306 db_user =...

Matplotlib绘制雷达图和三维图的示例代码

Matplotlib绘制雷达图和三维图的示例代码

1.雷达图 程序示例 '''1.空白极坐标图''' import matplotlib.pyplot as plt plt.polar() plt.show()...

django自带serializers序列化返回指定字段的方法

django orm 有个defer方法,指定模型排除的字段。 如下返回的Queryset, 排除‘username', 'id'。 users=models.UserInfo.ob...