在Python中移动目录结构的方法

yipeiwu_com6年前Python基础

来源:http://stackoverflow.com/questions/3806562/ways-to-move-up-and-down-the-dir-structure-in-python

#Moving up/down dir structure
print os.listdir('.') # current level
print os.listdir('..') # one level up
print os.listdir('../..') # two levels up
 
# more complex example:
# This will walk the file system beginning in the directory the script is run from. It 
# deletes the empty directories at each level
 
for root, dirs, files in os.walk(os.getcwd()):
  for name in dirs:
    try:
      os.rmdir(os.path.join(root, name))
    except WindowsError:
      print 'Skipping', os.path.join(root, name)			

This will walk the file system beginning in the directory the script is run from. It deletes the empty directories at each level.

相关文章

Sublime开发python程序的示例代码

本文介绍了Sublime开发python程序的示例代码,分享给大家,具体如下: 下载、安装Python程序 https://www.python.org/downloads/ 下载、安...

Python 动态导入对象,importlib.import_module()的使用方法

背景 一个函数运行需要根据不同项目的配置,动态导入对应的配置文件运行。 解决 文件结构 a #文件夹 │a.py │__init__.py b #文件夹 │b.py │__i...

Python学生信息管理系统修改版

在学习之前先要了解sqlite游标的使用方法python使用sqlite3时游标的使用方法 继上篇博客Python实现学生信息管理系统后,我就觉得写的太复杂了,然后又是一通优化、优化...

Python实现邮件的批量发送的示例代码

1 发送文本信息 '''加密发送文本邮件''' def sendEmail(from_addr,password,to_addr,smtp_server): try: msg...

解决在pycharm运行代码,调用CMD窗口的命令运行显示乱码问题

解决在pycharm运行代码,调用CMD窗口的命令运行显示乱码问题

以下面的代码为例 import os os.system(“ping www.baidu.com”) 在pycharm界面运行后会显示乱码,如下图所示 网上很多无用的教程,说什么...