Article From:https://www.cnblogs.com/112358nizhipeng/p/9557218.html

osModular

 1 import os
 2 import time
 3 print(os.getcwd()) #pyThe execution file path is different from that under console.
 4 
 5 os.chdir(r'/home/nizhipeng') #Changing the current script directory; equivalent to CD, changing the working environment.
 6 print(os.getcwd())
 7 
 8 print(os.curdir) #.Returns the current directory: (".").
 9 print(os.pardir) #..Gets the name of the parent directory of the current directory: (",").
10 
11 #Viewing changes under the nizhipeng file
12 os.makedirs('abc/alex')#The current working directory has changed to / home / nizhipeng, so a multilayer recursive directory ABC / Alex is generated in its path
13 time.sleep(.5)
14 os.removedirs('abc/alex')#Delete only empty folder
15 
16 #Viewing changes under the nizhipeng file
17 os.mkdir('efd')    #Generate single level directory; equivalent to MKDIR dirname in shell
18 time.sleep(.5)
19 os.rmdir('efd')    #Delete single level empty directory. If directory is not empty, it can not be deleted and reported wrong. It is equivalent to rmdir dirname in shell.
20 
21 dirs = os.listdir(r'/home/nizhipeng/PycharmProjects/learnPython/week4')
22 print(dirs) #List all the files and subdirectories under the specified directory, including hidden files, and print them in a list.
23 
24 #os.remove('ppt.py')# In the /home/nizhipeng workspace, you can only delete files, not delete folders.
25 
26 #os.rename("ww","www") #Rename file / directory in /home/nizhipeng workspace
27 
28 info = os.stat('./www')
29 print(info)
30 
31 print(os.sep) #Output OS-specific path separator // in linux, but \\\\\\\\\\\\\\\\\\\\\\
32 
33 print(os.system("dir"))
34 
35 print(os.environ)  #Get system environment variables
36 
37 print(os.path.abspath('./wwww')) #Returns the absolute path of path normalization.
38 
39 path = os.path.split(r'/home/nizhipeng/PycharmProjects/learnPython/week4/day5/os_module.py')
40 print(path) #Split path into directory and file name two tuple to return (path, file).
41 
42 #Get the upper level path of absolute path.
43 print(os.path.dirname('/home/nizhipeng/PycharmProjects/learnPython/week4/day5'))  #Returns the directory of path. It's actually the first element of os.path.split (path).

 The twelfth row generates multi-level files under the working directory.

Implementation results:

/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             PublicCatkin_ws kinect_ros publish templateCeres-solver-master    libfreenect     pycharm-community-2018.2.1  videoClion-2017.3.4        libfreenect2     PycharmProjects         pictureCLion-2017.3.4.tar.gz    libvisensor     Qt5.11.0             FileData mynt-eye     ros_imu             downloadDBow3 octomap-devel     ros_workspace             MusicExamples.desktop opencv-3.3.0     serial-master             desktopZeroEnviron ({'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

 

 

Reference: http://www.cnblogs.com/alex3714/articles/5161349.html

Link of this Article: Python (OS module)

Leave a Reply

Your email address will not be published. Required fields are marked *