# ********************day22_1-课前上节复习+os模块 *******************
# ********************day22_1-课前上节复习+os模块 *******************
# ********************day22_1-课前上节复习+os模块 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# ------------------------------------------------------------
# # 1、from 目录 import 文件
# # # 一般来说,涉及的跨目录调用一般是嵌套2层
目录结构:
day22_os_json_re_etc_MoKuai\day22_os_json_re_etc_MoKuai.py( 该运行文件 )
day22_os_json_re_etc_MoKuai\binin.py
# ------------------------------------------------------------# ------------------------------------------------------------
# # 2、sys修改环境变量(临时)
# # # 格式:sys.path.append("路径字符串") 或者 sys.path.append(r"路径字符串")
# # # 只有在运行该程序的时候才进行临修改,永久性修改的话要去“系统属性中修改”
# # # 永久性修改:win7电脑怎样修改环境变量_百度经验
# # # https://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html
# ------------------------------------------------------------# ------------------------------------------------------------
# # 3、os.path.dirname(__file__) 与 os.path.abspath(__file__)
# # # 返回的是.py文件的目录
# # # 1) os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用 - 洛水浮生 - 博客园
# # # http://www.cnblogs.com/luoshuifusheng/p/9207238.html
# # # 2) python中的os.path.dirname(__file__)的使用 - CSDN博客
# # # https://blog.csdn.net/u011760056/article/details/46969883
# # #
# ------------------------------------------------------------# ------------------------------------------------------------
# # 4、os.path.dirname(__file__) 与 os.path.abspath(__file__) 应用
# # # 在文件工程拷贝中,需要保证工程到其他的电脑系统中,不会出现文件路径问题,
# # # 可用下面的方式来解决
# # 4.1 __file__ 这个拿到的是当前的文件名,在pycharm编译器中,编译器自动帮你添加了文件所在的路径
# ------------------------------------------------------------# ------------------------------------------------------------
# # 5、os.getcwd()
# # # os.getcwd() 方法用于返回当前工作目录。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 6、os.chdir()
# # # os.chdir() 方法用于改变当前工作目录到指定的路径。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 7、os.chdir() 、 os.path.dirname 、 os.path.abspath() 与 __file__应用
# # # 获取执行文件所在的目录以及上级目录
# ------------------------------------------------------------# ------------------------------------------------------------
# # 8、os.curdir 返回当前目录: ('.')
# # # 返回当前目录: ('.'),它就是一个"."
# ------------------------------------------------------------# ------------------------------------------------------------
# # 9、os.pardir
# # # 获取当前目录的父目录字符串名:('..')
# ------------------------------------------------------------# ------------------------------------------------------------
# # 10、os.makedirs
# # # 可生成多层递归目录,需要注意的是,这个是在当前目录下所创建的
# # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1"
# ------------------------------------------------------------# ------------------------------------------------------------
# # 11、os.removedirs
# # # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
# # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1"
# ------------------------------------------------------------# ------------------------------------------------------------
# # 12、os.mkdir
# # # 生成单级目录;相当于shell中mkdir dirname
# # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1"
# ------------------------------------------------------------# ------------------------------------------------------------
# # 13、os.rmdir
# # # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
# # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1"
# ------------------------------------------------------------# ------------------------------------------------------------
# # 14、os.listdir('dirname')
# # # 列出指定目录下的所有文件和子目录,包括隐藏文件,并以 列表方式打印
# ------------------------------------------------------------# ------------------------------------------------------------
# # 15、os.remove()
# # # 删除一个文件
# ------------------------------------------------------------# ------------------------------------------------------------
# # 16、os.rename()
# # # rename 重命名文件/目录
# # # Python os.rename() 方法 | 菜鸟教程
# # # http://www.runoob.com/python/os-rename.html
# ------------------------------------------------------------# ------------------------------------------------------------
# # 17、os.stat('path/filename') 获取文件/目录信息
# # # 获取文件/目录信息
# ------------------------------------------------------------# ------------------------------------------------------------
# # 18、os.sep
# # # 输出操作系统特定的路径分隔符,win下为"\"(双斜杆,是因为第一个是转义字符),Linux下为"/"
# ------------------------------------------------------------# ------------------------------------------------------------
# # 19、os.linesep
# # # 输出当前平台使用的行终止符,win下为" ",Linux下为" "
# ------------------------------------------------------------# ------------------------------------------------------------
# # 20、os.pathsep
# # # 输出用于分割文件路径的字符串 win下为;,Linux下为:
# ------------------------------------------------------------# ------------------------------------------------------------
# # 21、os.name
# # # 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
# ------------------------------------------------------------# ------------------------------------------------------------
# # 22、os.system("bash command")
# # # 运行shell命令,直接显示
# # # 输出的结果和在控制台上运行的效果是一样的,只不过是用到了python os的模块来完成。
# # # 分享python os.system一点心得 - 老王python - 博客园
# # # https://www.cnblogs.com/wanpython/archive/2010/12/15/1906468.html
# ------------------------------------------------------------# # 23、os.environ
# # # 获取系统环境变量
# # # 当前系统的环境变量的查看方式:
# # # win7电脑怎样修改环境变量_百度经验
# # # https://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html
# ------------------------------------------------------------# ------------------------------------------------------------
# # 24、os.path.split(path)
# # # 将path分割成目录和文件名二元组返回
# ------------------------------------------------------------# ------------------------------------------------------------
# # 25、os.path.basename(path)
# # # 返回path最后的文件名。如何path以/或结尾,那么就会返回空值。
# # # 即os.path.split(path)的第二个元素
# ------------------------------------------------------------# ------------------------------------------------------------
# # 26、os.path.exists(path)
# # # 如果path存在,返回True;如果path不存在,返回False
# ------------------------------------------------------------# ------------------------------------------------------------
# # 27、os.path.isabs(path)
# # # 如果path是绝对路径,返回True
# ------------------------------------------------------------# ------------------------------------------------------------
# # 28、os.path.isfile(path)
# # # 如果path是一个存在的文件,返回True。否则返回False
# ------------------------------------------------------------# ------------------------------------------------------------
# # 29、os.path.isdir(path)
# # # 如果path是一个存在的目录,则返回True。否则返回False
# ------------------------------------------------------------# ------------------------------------------------------------
# # 30、os.path.join(path1[, path2[, ...]])
# # # 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
# ------------------------------------------------------------# ------------------------------------------------------------
# # 31、os.path.getatime(path)
# # # 返回path所指向的文件或者目录的最后存取时间
# ------------------------------------------------------------# ------------------------------------------------------------
# # 32、os.path.getmtime(path)
# # # 返回path所指向的文件或者目录的最后修改时间
# ------------------------------------------------------------
# 01 模块的补充 # 01 模块的补充 # 01 模块的补充 """ # ------------------------------------------------------------ # # 1、from 目录 import 文件 # # # 一般来说,涉及的跨目录调用一般是嵌套2层 目录结构: day22_os_json_re_etc_MoKuai\day22_os_json_re_etc_MoKuai.py( 该运行文件 ) day22_os_json_re_etc_MoKuai\binin.py # ------------------------------------------------------------ """ # from bin import bin # if __name__ == "__main__": # print(bin.hello()) # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # here is bin directory! # # hi! I am function hello! # # None # # # # Process finished with exit code 0 # 02 sys修改环境变量 # 02 sys修改环境变量 # 02 sys修改环境变量 """ # ------------------------------------------------------------ # # 2、sys修改环境变量(临时) # # # 格式:sys.path.append("路径字符串") 或者 sys.path.append(r"路径字符串") # # # 只有在运行该程序的时候才进行临修改,永久性修改的话要去“系统属性中修改” # # # 永久性修改:win7电脑怎样修改环境变量_百度经验 # # # https://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html # ------------------------------------------------------------ """ # # import sys # print("before: ",sys.path) # sys.path.append("day22_os_json_re_etc_MoKuai/bin/bin.py") # print("after: ",sys.path) # # sys.path.append(r"day22_os_json_re_etc_MoKuai/bin/bin.py") # 正则的时候会有这个知识点 # print("after: ",sys.path) # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # before: ['D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\Anaconda3\python36.zip', 'D:\Anaconda3\DLLs', 'D:\Anaconda3\lib', 'D:\Anaconda3', 'D:\Anaconda3\lib\site-packages', 'D:\Anaconda3\lib\site-packages\win32', 'D:\Anaconda3\lib\site-packages\win32\lib', 'D:\Anaconda3\lib\site-packages\Pythonwin', 'D:\Program Files (x86)\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend'] # # after: ['D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\Anaconda3\python36.zip', 'D:\Anaconda3\DLLs', 'D:\Anaconda3\lib', 'D:\Anaconda3', 'D:\Anaconda3\lib\site-packages', 'D:\Anaconda3\lib\site-packages\win32', 'D:\Anaconda3\lib\site-packages\win32\lib', 'D:\Anaconda3\lib\site-packages\Pythonwin', 'D:\Program Files (x86)\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend', 'day22_os_json_re_etc_MoKuai/bin/bin.py'] # # after: ['D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\Anaconda3\python36.zip', 'D:\Anaconda3\DLLs', 'D:\Anaconda3\lib', 'D:\Anaconda3', 'D:\Anaconda3\lib\site-packages', 'D:\Anaconda3\lib\site-packages\win32', 'D:\Anaconda3\lib\site-packages\win32\lib', 'D:\Anaconda3\lib\site-packages\Pythonwin', 'D:\Program Files (x86)\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend', 'day22_os_json_re_etc_MoKuai/bin/bin.py', 'day22_os_json_re_etc_MoKuai/bin/bin.py'] # # D:/C_cache/py/day22_os_json_re_etc_MoKuai # # # # Process finished with exit code 0 # 03 BASEDIR的介绍 # 03 BASEDIR的介绍 # 03 BASEDIR的介绍 """ # ------------------------------------------------------------ # # 3、os.path.dirname(__file__) 与 os.path.abspath(__file__) # # # 返回的是.py文件的目录 # # # 1) os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用 - 洛水浮生 - 博客园 # # # http://www.cnblogs.com/luoshuifusheng/p/9207238.html # # # 2) python中的os.path.dirname(__file__)的使用 - CSDN博客 # # # https://blog.csdn.net/u011760056/article/details/46969883 # # # # ------------------------------------------------------------ """ # # import os # print("__file__: ",__file__) # # 返回脚本的路径( 执行文件下的前一级目录,这个是基于当前系统下的绝对路径, # # 当工程的位置修改后,就可能导致出错) # print(os.path.dirname(__file__)) # # # 返回的是.py文件的绝对路径(完整路径) # print( os.path.abspath(__file__) ) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # __file__: # # D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # D:/C_cache/py/day22_os_json_re_etc_MoKuai # # D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 4、os.path.dirname(__file__) 与 os.path.abspath(__file__) 应用 # # # 在文件工程拷贝中,需要保证工程到其他的电脑系统中,不会出现文件路径问题, # # # 可用下面的方式来解决 # # 4.1 __file__ 这个拿到的是当前的文件名,在pycharm编译器中,编译器自动帮你添加了文件所在的路径 # ------------------------------------------------------------ """ # import os,sys # # # # 这种虽然与下面的方式,似乎区别不大,但是实际上,这种方式在工程拷贝到其他电脑的时候 # # # 非常容易出错,因此采用的是下面的那种方式 # BASE_DIR1 = os.path.dirname(os.path.dirname((__file__))) # sys.path.append(BASE_DIR1) # print(BASE_DIR1) # print(sys.path) # # # # 该的前一级目录(这个在工程拷贝到其他的系统中常用)<<<<<==========推荐使用 # BASE_DIR2 = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # sys.path.append(BASE_DIR2) # print(BASE_DIR2) # print(sys.path) # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # D:/C_cache/py # # ['D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\Anaconda3\python36.zip', 'D:\Anaconda3\DLLs', 'D:\Anaconda3\lib', 'D:\Anaconda3', 'D:\Anaconda3\lib\site-packages', 'D:\Anaconda3\lib\site-packages\win32', 'D:\Anaconda3\lib\site-packages\win32\lib', 'D:\Anaconda3\lib\site-packages\Pythonwin', 'D:\Program Files (x86)\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend', 'D:/C_cache/py'] # # D:C_cachepy # # ['D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'D:\Anaconda3\python36.zip', 'D:\Anaconda3\DLLs', 'D:\Anaconda3\lib', 'D:\Anaconda3', 'D:\Anaconda3\lib\site-packages', 'D:\Anaconda3\lib\site-packages\win32', 'D:\Anaconda3\lib\site-packages\win32\lib', 'D:\Anaconda3\lib\site-packages\Pythonwin', 'D:\Program Files (x86)\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend', 'D:/C_cache/py', 'D:\C_cache\py'] # # # # Process finished with exit code 0 # 04 os模块的绍介 # 04 os模块的绍介 # 04 os模块的绍介 """ # ------------------------------------------------------------ # # 5、os.getcwd() # # # os.getcwd() 方法用于返回当前工作目录。 # ------------------------------------------------------------ """ # # import os # print( os.getcwd()) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # D:C_cachepyday22_os_json_re_etc_MoKuai # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 6、os.chdir() # # # os.chdir() 方法用于改变当前工作目录到指定的路径。 # ------------------------------------------------------------ """ # # import os # print( "当前目录: ",os.getcwd()) # print("返回上级目录:" ,os.chdir("..")) # print( "当前目录: ",os.getcwd()) # # print("D:\: ", os.chdir("D:\")) # print( "当前目录: ",os.getcwd()) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 当前目录: D:C_cachepyday22_os_json_re_etc_MoKuai # # 返回上级目录: None # # 当前目录: D:C_cachepy # # D:: None # # 当前目录: D: # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 7、os.chdir() 、 os.path.dirname 、 os.path.abspath() 与 __file__应用 # # # 获取执行文件所在的目录以及上级目录 # ------------------------------------------------------------ """ # # import os # print( "当前目录: ",os.getcwd()) # print( "执行文件的绝对路径: ",os.path.abspath(__file__) ) # # os.chdir(os.path.dirname(os.path.abspath(__file__))) # print( "返回执行文件的所在的目录: ",os.getcwd()) # # os.chdir(os.path.dirname( os.path.dirname(os.path.abspath(__file__))) ) # print( "返回执行文件的上一级目录: ",os.getcwd()) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 当前目录: D:C_cachepyday22_os_json_re_etc_MoKuai # # 执行文件的绝对路径: D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py # # 返回执行文件的所在的目录: D:C_cachepyday22_os_json_re_etc_MoKuai # # 返回执行文件的上一级目录: D:C_cachepy # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 8、os.curdir 返回当前目录: ('.') # # # 返回当前目录: ('.'),它就是一个"." # ------------------------------------------------------------ """ # # import os # print(os.getcwd()) # print(os.curdir) # # print(os.getcwd()) # print(os.chdir("d:\")) # # print(os.getcwd()) # os.chdir( os.curdir ) # print(os.getcwd()) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # D:C_cachepyday22_os_json_re_etc_MoKuai # # . # # D:C_cachepyday22_os_json_re_etc_MoKuai # # None # # d: # # d: # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 9、os.pardir # # # 获取当前目录的父目录字符串名:('..') # ------------------------------------------------------------ """ # # import os # print(os.pardir) # # print(os.getcwd()) # print(os.chdir(os.pardir)) # print(os.getcwd()) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # .. # # D:C_cachepyday22_os_json_re_etc_MoKuai # # None # # D:C_cachepy # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 10、os.makedirs # # # 可生成多层递归目录,需要注意的是,这个是在当前目录下所创建的 # # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1" # ------------------------------------------------------------ """ # # import os # os.makedirs("a/b/c") # os.makedirs("a1\b1\c1") # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 11、os.removedirs # # # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 # # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1" # ------------------------------------------------------------ """ # # import os # # 当前文件夹下创建文件夹 # os.makedirs("a/b/c") # os.makedirs("a1\b1\c1") # # 对上面的文件夹删除 # os.removedirs("a/b/c") # os.removedirs("a1\b1\c1") # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 12、os.mkdir # # # 生成单级目录;相当于shell中mkdir dirname # # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1" # ------------------------------------------------------------ """ # # import os # os.mkdir("mkfile") # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 13、os.rmdir # # # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname # # # 分隔符有两种表达,linux: "a/b/c"; windows:"a1\b1\c1" # ------------------------------------------------------------ """ import os # 当前文件夹下创建文件夹 # os.mkdir("mkfile") # 并在 mkfile1 文件夹中文一个文件 # os.mkdir("mkfile1") # 删除上述创建的文件夹 # os.rmdir("mkfile") # 这里会报错,因为文件目录不是空的 # os.rmdir("mkfile1") # # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 14、os.listdir('dirname') # # # 列出指定目录下的所有文件和子目录,包括隐藏文件,并以 列表方式打印 # ------------------------------------------------------------ """ # # import os # print( os.listdir(os.curdir) ) # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # ['.idea', 'bin', 'day22_os_json_re_etc_MoKuai.py', 'mkfile', 'mkfile1'] # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 15、os.remove() # # # 删除一个文件 # ------------------------------------------------------------ """ # # import os # print(os.listdir(os.curdir)) # print(os.remove('a')) # print(os.listdir(os.curdir)) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # ['.idea', 'a', 'bin', 'day22_os_json_re_etc_MoKuai.py', 'mkfile', 'mkfile1'] # # None # # ['.idea', 'bin', 'day22_os_json_re_etc_MoKuai.py', 'mkfile', 'mkfile1'] # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 16、os.rename() # # # rename 重命名文件/目录 # # # Python os.rename() 方法 | 菜鸟教程 # # # http://www.runoob.com/python/os-rename.html # ------------------------------------------------------------ """ # # import os # # os.makedirs("a1\b1") # print("目录结构:", os.listdir(os.getcwd())) # # # ("a1\b1")=====》("a1\b22") # os.rename("a1\b1","a1\b22") # 会报错 # # a1===>>a22 # os.rename("a1","a22") # # print("目录结构:", os.listdir(os.getcwd())) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 目录结构: ['.idea', 'a', 'a1', 'bin', 'day22_os_json_re_etc_MoKuai.py', 'mkfile', 'mkfile1'] # # 目录结构: ['.idea', 'a', 'a22', 'bin', 'day22_os_json_re_etc_MoKuai.py', 'mkfile', 'mkfile1'] # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 17、os.stat('path/filename') 获取文件/目录信息 # # # 获取文件/目录信息 # ------------------------------------------------------------ """ # # import os # # print(os.listdir(os.getcwd())) # # os.mkdir("new") # print(os.listdir(os.getcwd())) # # print(os.stat("new")) # print(type(os.stat("new")) ) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # ['.idea', 'a', 'a22', 'bin', 'day22_os_json_re_etc_MoKuai.py'] # # ['.idea', 'a', 'a22', 'bin', 'day22_os_json_re_etc_MoKuai.py', 'new'] # # os.stat_result(st_mode=16895, st_ino=8725724278321625, st_dev=3088873539, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1533522093, st_mtime=1533522093, st_ctime=1533522093) # # <class 'os.stat_result'> # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 18、os.sep # # # 输出操作系统特定的路径分隔符,win下为"\"(双斜杆,是因为第一个是转义字符),Linux下为"/" # ------------------------------------------------------------ """ # # import os # print(os.sep) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 19、os.linesep # # # 输出当前平台使用的行终止符,win下为" ",Linux下为" " # ------------------------------------------------------------ """ # # import os # # # 默认被编译器处理为“回车” # print((os.linesep),type(os.linesep)) # # 默认被编译器处理为“回车” # print(str(os.linesep)) # print(list(os.linesep)) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # # # <class 'str'> # # # # # # [' ', ' '] # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 20、os.pathsep # # # 输出用于分割文件路径的字符串 win下为;,Linux下为: # ------------------------------------------------------------ """ # # import os # print(os.pathsep) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # ; # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 21、os.name # # # 输出字符串指示当前使用平台。win->'nt'; Linux->'posix' # ------------------------------------------------------------ """ # # import os # print(os.name) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # nt # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 22、os.system("bash command") # # # 运行shell命令,直接显示 # # # 输出的结果和在控制台上运行的效果是一样的,只不过是用到了python os的模块来完成。 # # # 分享python os.system一点心得 - 老王python - 博客园 # # # https://www.cnblogs.com/wanpython/archive/2010/12/15/1906468.html # ------------------------------------------------------------ """ # # import os # print( os.system("bash command") ) # os.system("ping www.baidu.com") # # 结果是乱码的原因是因是pycharm中py的编码格式是 utf-8 的关系 # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 'bash' �����ڲ����ⲿ���Ҳ���ǿ����еij��� # # ���������ļ��� # # 1 # # # # ���� Ping www.a.shifen.com [14.215.177.38] ���� 32 �ֽڵ�����: # # ���� 14.215.177.38 �Ļظ�: �ֽ�=32 ʱ��=11ms TTL=56 # # ���� 14.215.177.38 �Ļظ�: �ֽ�=32 ʱ��=12ms TTL=56 # # ���� 14.215.177.38 �Ļظ�: �ֽ�=32 ʱ��=12ms TTL=56 # # ���� 14.215.177.38 �Ļظ�: �ֽ�=32 ʱ��=11ms TTL=56 # # # # 14.215.177.38 �� Ping ͳ����Ϣ: # # ���ݰ�: �ѷ��� = 4���ѽ��� = 4����ʧ = 0 (0% ��ʧ)�� # # �����г̵Ĺ���ʱ��(�Ժ���Ϊ��λ): # # ��� = 11ms��� = 12ms��ƽ�� = 11ms # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 23、os.environ # # # 获取系统环境变量 # # # 当前系统的环境变量的查看方式: # # # win7电脑怎样修改环境变量_百度经验 # # # https://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html # ------------------------------------------------------------ """ # # import os # print(os.path) # print(os.environ) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # <module 'ntpath' from 'D:\Anaconda3\lib\ntpath.py'> # # environ({'ALLUSERSPROFILE': 'C:\ProgramData', 'APPDATA': 'C:\Users\Roth\AppData\Roaming', 'COMMONPROGRAMFILES': 'C:\Program Files\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\Program Files (x86)\Common Files', 'COMMONPROGRAMW6432': 'C:\Program Files\Common Files', 'COMPUTERNAME': 'ROTH-PC', 'COMSPEC': 'C:\Windows\system32\cmd.exe', 'FP_NO_HOST_CHECK': 'NO', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\Users\Roth', 'LOCALAPPDATA': 'C:\Users\Roth\AppData\Local', 'LOGONSERVER': '\\ROTH-PC', 'MOZ_PLUGIN_PATH': 'D:\Program Files (x86)\Foxit Software\Foxit Reader\plugins\', 'NUMBER_OF_PROCESSORS': '4', 'OS': 'Windows_NT', 'PATH': 'D:\Anaconda3\DLLs;D:\Program Files (x86)\Python27;D:\Anaconda3;D:\Anaconda3\Library\mingw-w64\bin;D:\Anaconda3\Library\usr\bin;D:\Anaconda3\Library\bin;D:\Anaconda3\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files (x86)\Calibre2\;C:\Program Files\Microsoft VS Code\bin', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 69 Stepping 1, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '4501', 'PROGRAMDATA': 'C:\ProgramData', 'PROGRAMFILES': 'C:\Program Files', 'PROGRAMFILES(X86)': 'C:\Program Files (x86)', 'PROGRAMW6432': 'C:\Program Files', 'PSMODULEPATH': 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\', 'PUBLIC': 'C:\Users\Public', 'PYCHARM_HOSTED': '1', 'PYCHARM_MATPLOTLIB_PORT': '49727', 'PYTHONIOENCODING': 'UTF-8', 'PYTHONPATH': 'D:\Program Files (x86)\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend;D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'PYTHONUNBUFFERED': '1', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\Windows', 'TEMP': 'C:\Users\Roth\AppData\Local\Temp', 'TMP': 'C:\Users\Roth\AppData\Local\Temp', 'USERDOMAIN': 'Roth-PC', 'USERNAME': 'Roth', 'USERPROFILE': 'C:\Users\Roth', 'WINDIR': 'C:\Windows'}) # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 24、os.path.split(path) # # # 将path分割成目录和文件名二元组返回 # ------------------------------------------------------------ """ # # import os # # 实际上,__file__指的就是当前的执行文件名,只是在pycharm编译器中,编译器自动添加了执行文件的前面的路径 # print(os.path.split(__file__)) # # print(os.path.split(os.path.dirname(__file__))) # print(os.path.split(os.path.abspath(__file__))) # # print(os.path.split("D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # print(os.path.split(r"D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # 正则表达式 # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # ('D:/C_cache/py/day22_os_json_re_etc_MoKuai', 'day22_os_json_re_etc_MoKuai.py') # # ('D:/C_cache/py', 'day22_os_json_re_etc_MoKuai') # # ('D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'day22_os_json_re_etc_MoKuai.py') # # ('D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'day22_os_json_re_etc_MoKuai.py') # # ('D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'day22_os_json_re_etc_MoKuai.py') # # # # Process finished with exit code 0 # # """ # ------------------------------------------------------------ # # 25、os.path.basename(path) # # # 返回path最后的文件名。如何path以/或结尾,那么就会返回空值。 # # # 即os.path.split(path)的第二个元素 # ------------------------------------------------------------ """ # # import os # print(os.path.split(__file__)) # print(os.path.basename(__file__)) # # print(os.path.split(os.path.dirname(__file__))) # print(os.path.basename(os.path.dirname(__file__))) # # # print(os.path.split(os.path.abspath(__file__))) # print(os.path.basename(os.path.abspath(__file__))) # # print(os.path.split(r"D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # 正则表达式 # print(os.path.basename(r"D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # 正则表达式 # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # ('D:/C_cache/py/day22_os_json_re_etc_MoKuai', 'day22_os_json_re_etc_MoKuai.py') # # day22_os_json_re_etc_MoKuai.py # # ('D:/C_cache/py', 'day22_os_json_re_etc_MoKuai') # # day22_os_json_re_etc_MoKuai # # ('D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'day22_os_json_re_etc_MoKuai.py') # # day22_os_json_re_etc_MoKuai.py # # ('D:\C_cache\py\day22_os_json_re_etc_MoKuai', 'day22_os_json_re_etc_MoKuai.py') # # day22_os_json_re_etc_MoKuai.py # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 26、os.path.exists(path) # # # 如果path存在,返回True;如果path不存在,返回False # ------------------------------------------------------------ """ # # import os # print(os.path.exists("D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # print(os.path.exists(r"D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # 正则表达式 # # txt文件是不存在的 # print(os.path.exists(r"D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.txt")) # 正则表达式 # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # True # # True # # False # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 27、os.path.isabs(path) # # # 如果path是绝对路径,返回True # ------------------------------------------------------------ """ # # import os # # print("__file__ ",os.path.isabs(__file__)) # print("dirname(__file__) ",os.path.isabs(os.path.dirname(__file__))) # print("abspath(__file__) ",os.path.isabs(os.path.abspath(__file__))) # print("os.curdir ",os.path.isabs(os.curdir)) # print("输入已存在的文件路径 ",os.path.isabs(r"D:C_cachepy # day22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # 正则表达式 # print("输入‘不’存在的文件路径 ",os.path.isabs(r"D:C_cachepy # day22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.txt")) # 正则表达式 # # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # __file__ True # # dirname(__file__) True # # abspath(__file__) True # # os.curdir False # # 输入已存在的文件路径 True # # 输入‘不’存在的文件路径 True # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 28、os.path.isfile(path) # # # 如果path是一个存在的文件,返回True。否则返回False # ------------------------------------------------------------ """ # # import os # print("输入已存在的文件路径 ",os.path.isfile("D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # # print("输入已存在的文件路径 ",os.path.isfile(r"D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.py")) # 正则表达式 # # 这个是后缀名不对,因此才不存在 # print("输入‘不’存在的文件路径 ",os.path.isfile(r"D:C_cachepyday22_os_json_re_etc_MoKuaiday22_os_json_re_etc_MoKuai.txt")) # 正则表达式 # # # 这个是一个文件夹 # print(os.path.isfile(r"D:C_cachepyday22_os_json_re_etc_MoKuai")) # 正则表达式 # # 已经存在的文件 # print(os.path.isfile(r"D:C_cachepyday22_os_json_re_etc_MoKuaia")) # 正则表达式 # print(os.path.isfile(r"D:C_cachepyday22_os_json_re_etc_MoKuai.txt")) # 正则表达式 # print(os.path.isfile(r"D:C_cachepyday22_os_json_re_etc_MoKuaid.py")) # 正则表达式 # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 输入已存在的文件路径 True # # 输入已存在的文件路径 True # # 输入‘不’存在的文件路径 False # # False # # True # # True # # True # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 29、os.path.isdir(path) # # # 如果path是一个存在的目录,则返回True。否则返回False # ------------------------------------------------------------ """ # # import os # # # 这个是一个存在的文件夹 # print(os.path.isdir(r"D:C_cachepyday22_os_json_re_etc_MoKuai")) # 正则表达式 # # # 这个是一个‘不’存在的文件夹 # print(os.path.isdir(r"D:C_cachepyday22_os_json_re_etc_MoKuai99999")) # 正则表达式 # # # # 已经存在的文件 # print(os.path.isdir(r"D:C_cachepyday22_os_json_re_etc_MoKuaia")) # 正则表达式 # print(os.path.isdir(r"D:C_cachepyday22_os_json_re_etc_MoKuai.txt")) # 正则表达式 # print(os.path.isdir(r"D:C_cachepyday22_os_json_re_etc_MoKuaid.py")) # 正则表达式 # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # True # # False # # False # # False # # False # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 30、os.path.join(path1[, path2[, ...]]) # # # 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 # ------------------------------------------------------------ 目录结构: D:C_cachepyday22_os_json_re_etc_MoKuaia221 ==>>下面是演示拼接上面的目录 其中,方式一是不推荐使用的,推荐使用方式二 """ # # import os # # 方式一 # tmp = "D:\C_cachepy\day22_os_json_re_etc_MoKuai" + "\" + "a22\b1" # # 方式二 # a = "D:\C_cachepy\day22_os_json_re_etc_MoKuai" # b = "a22\b1" # c = os.path.join(a,b) # # print("字符串加法处理: ",tmp) # print("os.path.join方法: ",c) # # # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 字符串加法处理: D:C_cachepyday22_os_json_re_etc_MoKuaia221 # # os.path.join方法: D:C_cachepyday22_os_json_re_etc_MoKuaia221 # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 31、os.path.getatime(path) # # # 返回path所指向的文件或者目录的最后存取时间 # ------------------------------------------------------------ """ # # import os # # print(os.path.getatime("D:\C_cache\py\day22_os_json_re_etc_MoKuai\b.txt")) # print(os.path.getatime("D:\C_cache\py\day22_os_json_re_etc_MoKuai\a22")) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 1533538486.7024333 # # 1533521630.9828572 # # # # Process finished with exit code 0 """ # ------------------------------------------------------------ # # 32、os.path.getmtime(path) # # # 返回path所指向的文件或者目录的最后修改时间 # ------------------------------------------------------------ """ # # import os # # print(os.path.getmtime("D:\C_cache\py\day22_os_json_re_etc_MoKuai\b.txt")) # print(os.path.getmtime("D:\C_cache\py\day22_os_json_re_etc_MoKuai\a22")) # # # D:Anaconda3python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_os_json_re_etc_MoKuai.py # # 1533538486.7024333 # # 1533521630.9828572 # # # # Process finished with exit code 0