os模块
1 import os 2 import time 3 print(os.getcwd()) #py执行文件路径,和在console下不一样 4 5 os.chdir(r'/home/nizhipeng') #改变当前脚本目录;相当于cd,工作环境发生改变 6 print(os.getcwd()) 7 8 print(os.curdir) #.返回当前目录: ('.') 9 print(os.pardir) #..获取当前目录的父目录字符串名:('..') 10 11 #在nizhipeng文件下查看变化 12 os.makedirs('abc/alex')#当前工作目录已改变,为/home/nizhipeng,所以在其路径下生成了多层递归目录 abc/alex 13 time.sleep(.5) 14 os.removedirs('abc/alex')#只删除空文件夹 15 16 #在nizhipeng文件下查看变化 17 os.mkdir('efd') #生成单级目录;相当于shell中mkdir dirname 18 time.sleep(.5) 19 os.rmdir('efd') #删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname 20 21 dirs = os.listdir(r'/home/nizhipeng/PycharmProjects/learnPython/week4') 22 print(dirs) #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 23 24 #os.remove('ppt.py')# 在/home/nizhipeng 工作空间下,只能删文件,不能删文件夹 25 26 #os.rename("ww","www") #重命名文件/目录 在/home/nizhipeng 工作空间下 27 28 info = os.stat('./www') 29 print(info) 30 31 print(os.sep) #输出操作系统特定的路径分隔符 / ,在linux下为/,但是在win下为\,跨平台时会涉及到这种情况 32 33 print(os.system("dir")) 34 35 print(os.environ) #获取系统环境变量 36 37 print(os.path.abspath('./wwww')) #返回path规范化的绝对路径 38 39 path = os.path.split(r'/home/nizhipeng/PycharmProjects/learnPython/week4/day5/os_module.py') 40 print(path) #将path分割成目录和文件名二元组返回 (路径,文件) 41 42 #获得绝对路径上一层的路径 43 print(os.path.dirname('/home/nizhipeng/PycharmProjects/learnPython/week4/day5')) #返回path的目录。其实就是os.path.split(path)的第一个元素
第12行,在工作目录下生多级文件。
执行结果:
/home/nizhipeng/PycharmProjects/learnPython/week4/day5 /home/nizhipeng . .. ['day4', 'day5'] os.stat_result(st_mode=16893, st_ino=1079409, st_dev=2056, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1535548162, st_mtime=1535548153, st_ctime=1535548162) / Arduino freenect2 OpenNI-Linux-x64-2.2 slambook arduino-1.8.5 g2o-master ORB_SLAM2 Sophus building_editor_models kinect Pangolin www catkin_learning kinect_cal_data pcl-pcl-1.8.1 公共的 catkin_ws kinect_ros publish 模板 ceres-solver-master libfreenect pycharm-community-2018.2.1 视频 clion-2017.3.4 libfreenect2 PycharmProjects 图片 CLion-2017.3.4.tar.gz libvisensor Qt5.11.0 文档 data mynt-eye ros_imu 下载 DBow3 octomap-devel ros_workspace 音乐 examples.desktop opencv-3.3.0 serial-master 桌面 0 environ({'QT_QPA_PLATFORMTHEME': 'appmenu-qt5', 'VIRTUAL_ENV': '/home/nizhipeng/PycharmProjects/learnPython/venv', 'DEFAULTS_PATH': '/usr/share/gconf/ubuntu.default.path', 'XDG_CURRENT_DESKTOP': 'Unity', 'PYTHONIOENCODING': 'UTF-8', 'DISPLAY': ':0', 'UPSTART_SESSION': 'unix:abstract=/com/ubuntu/upstart-session/1000/1160', 'SHLVL': '0', 'GTK_IM_MODULE': 'fcitx', 'PS1': '(venv) ', 'LOGNAME': 'nizhipeng', 'LANG': 'zh_CN.UTF-8', 'XDG_SESSION_TYPE': 'x11', 'USER': 'nizhipeng', 'LANGUAGE': 'zh_CN:', 'XMODIFIERS': '@im=fcitx', 'COMPIZ_BIN_PATH': '/usr/bin/', 'XDG_MENU_PREFIX': 'gnome-', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg', 'UPSTART_EVENTS': 'xsession started', 'DESKTOP_SESSION': 'ubuntu', 'JOB': 'unity-settings-daemon', 'XDG_SEAT': 'seat0', 'GNOME_KEYRING_CONTROL': '', 'XDG_DATA_DIRS': '/usr/share/ubuntu:/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop:/var/lib/snapd/desktop', 'GDMSESSION': 'ubuntu', 'GTK2_MODULES': 'overlay-scrollbar', 'QT_ACCESSIBILITY': '1', 'GPG_AGENT_INFO': '/home/nizhipeng/.gnupg/S.gpg-agent:0:1', 'GDM_LANG': 'zh_CN', 'PYTHONUNBUFFERED': '1', 'PATH': '/home/nizhipeng/PycharmProjects/learnPython/venv/bin:/home/nizhipeng/bin:/home/nizhipeng/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin', 'COMPIZ_CONFIG_PROFILE': 'ubuntu', 'UPSTART_JOB': 'unity7', 'IM_CONFIG_PHASE': '1', 'XAUTHORITY': '/home/nizhipeng/.Xauthority', 'SESSION_MANAGER': 'local/nizhipeng:@/tmp/.ICE-unix/1473,unix/nizhipeng:/tmp/.ICE-unix/1473', 'XDG_SESSION_ID': 'c1', 'PYCHARM_HOSTED': '1', 'CLUTTER_IM_MODULE': 'xim', 'PYTHONPATH': '/home/nizhipeng/PycharmProjects/learnPython', 'SHELL': '/bin/bash', 'GIO_LAUNCHED_DESKTOP_FILE': '/home/nizhipeng/.local/share/applications/jetbrains-pycharm-ce.desktop', 'XDG_RUNTIME_DIR': '/run/user/1000', 'HOME': '/home/nizhipeng', 'UNITY_DEFAULT_PROFILE': 'unity', 'GIO_LAUNCHED_DESKTOP_FILE_PID': '12562', 'UNITY_HAS_3D_SUPPORT': 'true', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'XDG_SESSION_DESKTOP': 'ubuntu', 'QT_IM_MODULE': 'fcitx', 'SESSIONTYPE': 'gnome-session', 'INSTANCE': '', 'QT4_IM_MODULE': 'fcitx', 'XDG_SESSION_PATH': '/org/freedesktop/DisplayManager/Session0', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-tidHVpjjH9', 'XDG_SEAT_PATH': '/org/freedesktop/DisplayManager/Seat0', 'XDG_VTNR': '7', 'PWD': '/home/nizhipeng/PycharmProjects/learnPython/week4/day5', 'GTK_MODULES': 'gail:atk-bridge:unity-gtk-module', 'MANDATORY_PATH': '/usr/share/gconf/ubuntu.mandatory.path', 'XDG_GREETER_DATA_DIR': '/var/lib/lightdm-data/nizhipeng', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'QT_LINUX_ACCESSIBILITY_ALWAYS_ON': '1', 'SESSION': 'ubuntu', 'UPSTART_INSTANCE': '', 'GNOME_KEYRING_PID': ''}) /home/nizhipeng/wwww ('/home/nizhipeng/PycharmProjects/learnPython/week4/day5', 'os_module.py') /home/nizhipeng/PycharmProjects/learnPython/week4 Process finished with exit code 0