标签:变量 当前目录 code ima 特定 process odi shell smo
import sys import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR)os.getcwd()
print(os.getcwd()) # 结果 F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码\day22
os.chdir("test1") # 将当前脚本的工作目录变更为test1 print(os.getcwd()) #结果,当前脚本的工作目录就变更到test1下面了 F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码\day22\test1
os.chdir("..") # 返回上一级目录
print(os.getcwd())
# 结果
# F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码
print(os.curdir) # 结果 .
print(os.pardir) # 结果 ..
os.makedirs("dir1/dir2")
os.removedirs("dir1/dir2") # 会将上面创建的两个文件夹都删掉 # 如果dir1下面创建任意一个文件,则只删除dir2
os.mkdir("dir3")
os.rmdir("dir3")
# 指定目录,就只会列出该目录下的文件和子目录 print(os.listdir("dir1")) # 结果 # [‘1.py‘, ‘dir2‘] # 没指定目录,列出的是与当前脚本同级的目录和文件 print(os.listdir())
# 必须指定要删除的文件的目录 os.remove("dir1/1.py")
# 把dir1重命名为 dir3 os.rename("dir1","dir3")
# 看文件 print(os.stat("os_test.py")) # os.stat_result(st_mode=33206, st_ino=281474976734818, st_dev=1143308193, st_nlink=1, st_uid=0, st_gid=0, st_size=556, st_atime=1530249778, st_mtime=1481602274, st_ctime=1530249778) # 看目录 print(os.stat("test1/test2")) # os.stat_result(st_mode=16895, st_ino=562949953445493, st_dev=1143308193, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1530450149, st_mtime=1530450149, st_ctime=1530450149) # 常用的需要看的 st_size=0 # 看文件的大小 st_atime=1530450149 # 用户最近一次的访问时间 st_mtime=1530450149 # 用户最近一次的修改时间 这是经常用的 st_ctime=1530450149 # 看文件的创建时间
print(os.sep) # \
print(os.linesep)
print(os.pathsep) # ;
print(os.name) # nt
print(os.system("dir"))
print(os.environ) # 结果 environ({‘ALLUSERSPROFILE‘: ‘C:\\ProgramData‘, ‘APPDATA‘: ‘C:\\Users\\mamingchen\\AppData\\Roaming‘, ‘ASL.LOG‘: ‘Destination=file‘, ‘CLASSPATH‘: ‘".;C:\\Program Files\\Java\\jdk1.8.0_171\\lib;C:\\Program Files\\Java\\jdk1.8.0_171\\lib\\tools.jar";‘, ‘COMMONPROGRAMFILES‘: ‘C:\\Program Files (x86)\\Common Files‘, ‘COMMONPROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)\\Common Files‘, ‘COMMONPROGRAMW6432‘: ‘C:\\Program Files\\Common Files‘, ‘COMPUTERNAME‘: ‘5ZVF1G2-2‘, ‘COMSPEC‘: ‘C:\\Windows\\system32\\cmd.exe‘, ‘HOMEDRIVE‘: ‘C:‘, ‘HOMEPATH‘: ‘\\Users\\mamingchen‘, ‘JAVA_HOME‘: ‘C:\\Program Files\\Java\\jdk1.8.0_171‘, ‘JENKINS_HOME‘: ‘C:\\Users\\mamingchen‘, ‘LOCALAPPDATA‘: ‘C:\\Users\\mamingchen\\AppData\\Local‘, ‘LOGONSERVER‘: ‘\\\\DC02-SHQCL‘, ‘NUMBER_OF_PROCESSORS‘: ‘4‘, ‘ONEDRIVE‘: ‘C:\\Users\\mamingchen\\OneDrive‘, ‘OS‘: ‘Windows_NT‘, ‘PATH‘: ‘C:\\ProgramData\\Anaconda3;C:\\ProgramData\\Anaconda3\\Library\\mingw-w64\\bin;C:\\ProgramData\\Anaconda3\\Library\\usr\\bin;C:\\ProgramData\\Anaconda3\\Library\\bin;C:\\ProgramData\\Anaconda3\\Scripts;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\nodejs\\;E:\\Program Files\\Git\\cmd;C:\\Python\\Python36-32;C:\\Python\\Python36-32\\Scripts;C:\\Python\\Python36-32\\Scripts;C:\\Python\\Python36-32\\;"C:\\Users\\mamingchen\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files (x86)\\Java\\jre1.8.0_151\\bin;C:\\Program Files (x86)\\Java\\jre1.8.0_151\\jre\\bin";E:\\Users\\mamingchen\\AppData\\Local\\Programs\\Fiddler;"C:\\Program Files\\Java\\jdk1.8.0_171\\bin;C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\bin";C:\\Users\\mamingchen\\AppData\\Roaming\\npm‘, ‘PATHEXT‘: ‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC‘, ‘PROCESSOR_ARCHITECTURE‘: ‘x86‘, ‘PROCESSOR_ARCHITEW6432‘: ‘AMD64‘, ‘PROCESSOR_IDENTIFIER‘: ‘Intel64 Family 6 Model 142 Stepping 9, GenuineIntel‘, ‘PROCESSOR_LEVEL‘: ‘6‘, ‘PROCESSOR_REVISION‘: ‘8e09‘, ‘PROGRAMDATA‘: ‘C:\\ProgramData‘, ‘PROGRAMFILES‘: ‘C:\\Program Files (x86)‘, ‘PROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)‘, ‘PROGRAMW6432‘: ‘C:\\Program Files‘, ‘PSMODULEPATH‘: ‘C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules‘, ‘PUBLIC‘: ‘C:\\Users\\Public‘, ‘PYCHARM_HOSTED‘: ‘1‘, ‘PYCHARM_MATPLOTLIB_PORT‘: ‘53877‘, ‘PYTHONIOENCODING‘: ‘UTF-8‘, ‘PYTHONPATH‘: ‘C:\\Program Files\\JetBrains\\PyCharm 2018.1.2\\helpers\\pycharm_matplotlib_backend;F:\\workspace\\OldBoy‘, ‘PYTHONUNBUFFERED‘: ‘1‘, ‘SESSIONNAME‘: ‘Console‘, ‘SYSTEMDRIVE‘: ‘C:‘, ‘SYSTEMROOT‘: ‘C:\\Windows‘, ‘TEMP‘: ‘C:\\Users\\MAMING~1\\AppData\\Local\\Temp‘, ‘TMP‘: ‘C:\\Users\\MAMING~1\\AppData\\Local\\Temp‘, ‘USERDNSDOMAIN‘: ‘YIXIN.DK‘, ‘USERDOMAIN‘: ‘YIXIN‘, ‘USERDOMAIN_ROAMINGPROFILE‘: ‘YIXIN‘, ‘USERNAME‘: ‘mamingchen‘, ‘USERPROFILE‘: ‘C:\\Users\\mamingchen‘, ‘VBOX_INSTALL_PATH‘: ‘E:\\Program Files\\Oracle\\VirtualBox\\‘, ‘WINDIR‘: ‘C:\\Windows‘})
print(os.path.split("F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码\day22\os_test.py")) # 结果 (‘F:\\workspace\\OldBoy\\源码\\课件与源码\\python全栈3期-课件与源码\\day22源码\\day22课堂代码\\day22‘, ‘os_test.py‘) # 相当于拿的是上面的第一个元素 print(os.path.dirname("F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码\day22\os_test.py")) # F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码\day22 # 相当于拿的是上面的第二个元素 print(os.path.basename("F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码\day22\os_test.py")) # os_test.py
a = "F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码" b = r"day22\test1\test2" # 要把上面两个路径拼接起来 c = os.path.join(a, b) print(c) F:\workspace\OldBoy\源码\课件与源码\python全栈3期-课件与源码\day22源码\day22课堂代码\day22\test1\test2
标签:变量 当前目录 code ima 特定 process odi shell smo
原文地址:https://www.cnblogs.com/victorm/p/9251122.html