Open KeepInLove1006 opened 5 years ago
了解基本的os和sys模块以及os和sys模块的区别 os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口;sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境。
(1)查看当前路径
a= os.getcwd() print(a)
结果:D:\PYproduct\untitled1\OS
(2)列举目录下的所有文件。返回的是列表类型
b = os.listdir(os.getcwd()) print(b)
结果:['os_01.py', 'os_02.py', 'os_03.py']
(3)返回path的绝对路径
c = os.path.abspath('..') print(c)
结果:D:\PYproduct\untitled1
(4)查看路径的文件夹部分和文件名部分
将路径分解为(文件夹,文件名),返回的是元组类型
d = os.path.split('D:\\Users\\xxx\\PycharmProjects\\untitled1\\OS\\os_01.py') print(d)
结果:('D:\Users\xxx\PycharmProjects\untitled1\OS', 'os_01.py')
可以看出,若路径字符串最后一个字符是\,则只有文件夹部分有值
# d = os.path.split('D:\\Users\\xxx\\PycharmProjects\\untitled1\\OS\\') # print(d)
结果:('D:\Users\xxx\PycharmProjects\untitled1\OS', '')
(6)将path进行组合,若其中有绝对路径,则之前的path将被删除
f = os.path.join('D:\\Users\\伶猴\\PycharmProjects\\untitled1\\OS', 'D:\\Users\\xxx\\PycharmProjects\\untitled1') print(f)
结果:D:\Users\xxx\PycharmProjects\untitled1
(7)返回path中的文件夹部分,结果不包含'\'
\
g = os.path.dirname('D:\\Users\\xxx\\PycharmProjects\\untitled1\\OS\\os_01.py') print(g)
结果:D:\Users\xxx\PycharmProjects\untitled1\OS
(8)返回path中的文件名
i = os.path.basename('D:\\Users\\xxx\\PycharmProjects\\untitled1\\OS\\os_01.py') print(i)
结果:os_01.py
(9)文件或文件夹是否存在,返回True 或 False
c = os.path.exists('D:\\Users\\xxx\\PycharmProjects\\untitled1\\OS\\os_03.py') print(c)
结果:True
(10)文件或文件夹的大小,若是文件夹返回0
a = os.path.getsize('D:\\Users\\xxx\\PycharmProjects\\untitled1\\OS\\os_01.py') print(a)
结果:1109
(11)其他方法:
# os其他方法 print(os.chdir(r"D:\program\python\code\LYH_python\Day5"))#切换进入到哪个目录下 os.rename('users.xls','pass.xls')#os.rename(old,new)将old文件重命名为new os.remove('pass.xls')#删除文件 os.mkdir('dis')#创建单级空文件夹,目录 os.rmdir('dis/b')#删除单级空目录 os.makedirs('dis/b')#创建多层递归目录 os.removedirs('dis')#只能删除空的文件夹 print(os.listdir('dis'))#列出指定目录下的所有文件及文件夹,包括隐藏文件,并返回一个list os.system('ipconfig')#可以直接操作系统命令,os.system(command) res=os.popen('ipconfig').read()#执行操作系统命令
(1)可以通过dir()方法查看模块中可用的方法
print(dir(sys))
结果:['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
(2)sys.argv实现从程序外部向程序传递参数
for a in sys.argv: print(a)
结果:D:/PYproduct/untitled1/sys/sys_01.py
(3)sys.path 包含输入模块的目录名列表
print('\n\nThe PYTHONPATH is', sys.path, '\n\n')
结果:The PYTHONPATH is ['D:\\PYproduct\\untitled1\\sys', 'D:\\PYproduct\\untitled1', 'D:\\python3.7\\python37.zip', 'D:\\python3.7\\DLLs', 'D:\\python3.7\\lib', 'D:\\python3.7', 'D:\\untitled1', 'D:\\untitled1\\lib\\site-packages', 'D:\\python3.7\\lib\\site-packages', 'C:\\Program Files\\JetBrains\\PyCharm 2018.3.5\\helpers\\pycharm_matplotlib_backend']
The PYTHONPATH is ['D:\\PYproduct\\untitled1\\sys', 'D:\\PYproduct\\untitled1', 'D:\\python3.7\\python37.zip', 'D:\\python3.7\\DLLs', 'D:\\python3.7\\lib', 'D:\\python3.7', 'D:\\untitled1', 'D:\\untitled1\\lib\\site-packages', 'D:\\python3.7\\lib\\site-packages', 'C:\\Program Files\\JetBrains\\PyCharm 2018.3.5\\helpers\\pycharm_matplotlib_backend']
(4)获取系统当前编码,一般默认为ascii。
print(sys.getdefaultencoding())
结果:utf-8
(5)此时将系统默认编码设置为utf8。(见设置系统默认编码 )
print(sys.getfilesystemencoding())
(6)获取文件系统使用编码方式,Windows下返回’mbcs’,mac下返回’utf-8’
print(sys.platform)
结果:win32
其实os和sys还有很多种方法,我讲的只是其中的一小部分,很多还需要你们自己去挖掘,勤能补拙嘛。
附上一小部分代码:
# coding=gbk import sys # sys.exit([arg]) 程序中间的退出, arg=0为正常退出 def exitfunc(value): print(value) sys.exit(1) print("hello") try: sys.exit(1) except SystemExit as value: exitfunc(value) print("come?")
结果: hello 1
了解基本的os和sys模块以及os和sys模块的区别 os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口;sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境。
os模块:
(1)查看当前路径
结果:D:\PYproduct\untitled1\OS
(2)列举目录下的所有文件。返回的是列表类型
结果:['os_01.py', 'os_02.py', 'os_03.py']
(3)返回path的绝对路径
结果:D:\PYproduct\untitled1
(4)查看路径的文件夹部分和文件名部分
将路径分解为(文件夹,文件名),返回的是元组类型
结果:('D:\Users\xxx\PycharmProjects\untitled1\OS', 'os_01.py')
可以看出,若路径字符串最后一个字符是\,则只有文件夹部分有值
结果:('D:\Users\xxx\PycharmProjects\untitled1\OS', '')
(6)将path进行组合,若其中有绝对路径,则之前的path将被删除
结果:D:\Users\xxx\PycharmProjects\untitled1
(7)返回path中的文件夹部分,结果不包含'
\
'结果:D:\Users\xxx\PycharmProjects\untitled1\OS
(8)返回path中的文件名
结果:os_01.py
(9)文件或文件夹是否存在,返回True 或 False
结果:True
(10)文件或文件夹的大小,若是文件夹返回0
结果:1109
(11)其他方法:
sys模块:
(1)可以通过dir()方法查看模块中可用的方法
结果:
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
(2)sys.argv实现从程序外部向程序传递参数
结果:D:/PYproduct/untitled1/sys/sys_01.py
(3)sys.path 包含输入模块的目录名列表
结果:
The PYTHONPATH is ['D:\\PYproduct\\untitled1\\sys', 'D:\\PYproduct\\untitled1', 'D:\\python3.7\\python37.zip', 'D:\\python3.7\\DLLs', 'D:\\python3.7\\lib', 'D:\\python3.7', 'D:\\untitled1', 'D:\\untitled1\\lib\\site-packages', 'D:\\python3.7\\lib\\site-packages', 'C:\\Program Files\\JetBrains\\PyCharm 2018.3.5\\helpers\\pycharm_matplotlib_backend']
(4)获取系统当前编码,一般默认为ascii。
结果:utf-8
(5)此时将系统默认编码设置为utf8。(见设置系统默认编码 )
结果:utf-8
(6)获取文件系统使用编码方式,Windows下返回’mbcs’,mac下返回’utf-8’
结果:win32
其实os和sys还有很多种方法,我讲的只是其中的一小部分,很多还需要你们自己去挖掘,勤能补拙嘛。
附上一小部分代码:
结果: hello 1