位置:首页 > 软件操作教程 > 其它 > 其它 > 问题详情

python 2.7操作文件的方法

提问人:wx20251019143144发布时间:2026-10-27

      python 2.7操作文件的方法

image.png

      下载Windows x86-64 MSI installer.exe安装包进行安装后使用python命令打开python命令行界面

image.png

      在命令行中导入os库,有了os库就可以使用python指令操作操作系统相关操作

      导入命令:

      >>>import os

image.png

      使用python的os指令创建目录

      创建目录d:/hello/python

      >>>os.makedirs('d:/hello/python')

      显示目录

      >>>os.listdir('d:/hello')

image.png

      使用 os.path.exists判断文件夹是否存在

      >>>os.path.exists('d:/hello/python')

      返回True ,表示d:/hello/python文件夹存在

      >>>os.path.exists('d:/hello/python2')

      返回False ,表示d:/hello/python2文件夹不存在

image.png

      使用os.path.isdir判断是否为文件夹,如果返回True则表示文件夹存在

      >>>os.path.isdir('d:/hello/python')

      返回True ,表示d:/hello/python文件夹存在

      >>>os.path.isdir('d:/hello/python2')

      返回False ,表示d:/hello/python2文件夹不存在

image.png

继续查找其他问题的答案?

相关视频回答
回复(0)
返回顶部