python模块

Posted cecelia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python模块相关的知识,希望对你有一定的参考价值。

1.OS模块

1 print(os.getcwd())   #获取当前工作目录
2 os.chdir(E:\python练习\python35学习)   #更改目录
3 print(os.getcwd())

E:\python练习\python35学习\Day5
E:\python练习\python35学习

1 print(os.curdir)  #当前目录
2 print(os.pardir)  #父级(上一级)目录
3 print(os.sep)   #路径分隔符
4 print(os.pathsep)  #文件路径分割符
5 print(os.name)   #当前使用平台名称,win-->‘nt‘,Linux-->‘posix‘
6 print(os.environ)  #环境变量(以字典方式显示)

.
..
\
;
nt
environ({‘PROCESSOR_ARCHITECTURE‘: ‘AMD64‘, ‘OS‘: ‘Windows_NT‘, ‘SESSIONNAME‘: ‘Console‘, ‘VS140COMNTOOLS‘: ‘C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\14.0\\Common7\\Tools\\‘, ‘LOGONSERVER‘: ‘\\\\DESKTOP-HU5CSPT‘, ‘MOZ_PLUGIN_PATH‘: ‘C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\plugins\\‘, ‘PROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)‘, ‘PYTHONPATH‘: ‘E:\\python练习\\python35学习;D:\\Python\\Lib;F:\\pjproject-2.7\\tests\\pjsua‘, ‘PYCHARM_HOSTED‘: ‘1‘, ‘PYTHONUNBUFFERED‘: ‘1‘, ‘MSDEVDIR‘: ‘E:\\MSDev98‘, ‘_OLD_VIRTUAL_PROMPT‘: ‘$P$G‘, ‘PATHEXT‘: ‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC‘, ‘SYSTEMDRIVE‘: ‘C:‘, ‘_OLD_VIRTUAL_PATH‘: ‘C:\\Program Files\\Java\\jdk1.8.0\\bin;C:\\Program Files\\Java\\jdk1.8.0\\jre\\bin;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;D:\\MyDownloads\\MATLAB\\R2016a\\runtime\\win64;D:\\MyDownloads\\MATLAB\\R2016a\\bin;D:\\MyDownloads\\MATLAB\\R2016a\\polyspace\\bin;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;D:\\Matlab install\\runtime\\win64;D:\\Matlab install\\bin;D:\\Matlab install\\polyspace\\bin;C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin;F:\\Dsystem\\mysql-5.7.18-winx64\\bin;D:\\devlop\\MinGW\\bin;C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python36;D:\\Python3.5.2\\Scripts\\;D:\\Python3.5.2\\;D:\\Anaconda3-4;D:\\Anaconda3-4\\Scripts;D:\\Anaconda3-4\\Library\\bin;E:\\Tools\\WinNT;E:\\MSDev98\\Bin;E:\\Tools;C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python35;C:\\Program Files\\Java\\jdk1.7.0_17\\bin;C:\\Program Files\\Java\\jdk1.7.0_17\\jre\\bin;C:\\Program Files\\Java\\jdk1.8.0\\bin‘, ‘HOMEDRIVE‘: ‘C:‘, ‘PATH‘: ‘C:\\Program Files\\Java\\jdk1.8.0\\bin;C:\\Program Files\\Java\\jdk1.8.0\\jre\\bin;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;D:\\MyDownloads\\MATLAB\\R2016a\\runtime\\win64;D:\\MyDownloads\\MATLAB\\R2016a\\bin;D:\\MyDownloads\\MATLAB\\R2016a\\polyspace\\bin;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;D:\\Matlab install\\runtime\\win64;D:\\Matlab install\\bin;D:\\Matlab install\\polyspace\\bin;C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin;F:\\Dsystem\\mysql-5.7.18-winx64\\bin;D:\\devlop\\MinGW\\bin;C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python36;D:\\Python3.5.2\\Scripts\\;D:\\Python3.5.2\\;D:\\Anaconda3-4;D:\\Anaconda3-4\\Scripts;D:\\Anaconda3-4\\Library\\bin;E:\\Tools\\WinNT;E:\\MSDev98\\Bin;E:\\Tools;C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python35;C:\\Program Files\\Java\\jdk1.7.0_17\\bin;C:\\Program Files\\Java\\jdk1.7.0_17\\jre\\bin;C:\\Program Files\\Java\\jdk1.8.0\\bin;E:\\python练习\\python35学习\\venv\\Scripts‘, ‘PSMODULEPATH‘: ‘C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules‘, ‘COMMONPROGRAMW6432‘: ‘C:\\Program Files\\Common Files‘, ‘PROCESSOR_LEVEL‘: ‘6‘, ‘PUBLIC‘: ‘C:\\Users\\Public‘, ‘NUMBER_OF_PROCESSORS‘: ‘4‘, ‘VIRTUAL_ENV‘: ‘E:\\python缁冧範\\python35瀛︿範\\venv‘, ‘JAVA_HOME‘: ‘C:\\Program Files\\Java\\jre7‘, ‘USERPROFILE‘: ‘C:\\Users\\ASUS‘, ‘COMSPEC‘: ‘C:\\WINDOWS\\system32\\cmd.exe‘, ‘ALLUSERSPROFILE‘: ‘C:\\ProgramData‘, ‘APPDATA‘: ‘C:\\Users\\ASUS\\AppData\\Roaming‘, ‘WINDIR‘: ‘C:\\WINDOWS‘, ‘PROGRAMDATA‘: ‘C:\\ProgramData‘, ‘PROGRAMFILES‘: ‘C:\\Program Files‘, ‘MATLAB_JAVA‘: ‘C:\\Program Files\\Java\\jre7‘, ‘USERDOMAIN_ROAMINGPROFILE‘: ‘DESKTOP-HU5CSPT‘, ‘PROMPT‘: ‘(venv) $P$G‘, ‘PROCESSOR_IDENTIFIER‘: ‘Intel64 Family 6 Model 78 Stepping 3, GenuineIntel‘, ‘MW_MINGW64_LOC‘: ‘D:\\TDM-GCC-64‘, ‘LIB‘: ‘E:\\Microsoft Visual Studio\\VC98\\mfc\\lib;E:\\Microsoft Visual Studio\\VC98\\lib‘, ‘COMPUTERNAME‘: ‘DESKTOP-HU5CSPT‘, ‘DXSDK_DIR‘: ‘D:\\DirectX SDK\\Include‘, ‘PYTHONIOENCODING‘: ‘UTF-8‘, ‘LOCALAPPDATA‘: ‘C:\\Users\\ASUS\\AppData\\Local‘, ‘USERNAME‘: ‘ASUS‘, ‘SYSTEMROOT‘: ‘C:\\WINDOWS‘, ‘TMP‘: ‘C:\\Users\\ASUS\\AppData\\Local\\Temp‘, ‘USERDOMAIN‘: ‘DESKTOP-HU5CSPT‘, ‘INCLUDE‘: ‘E:\\Microsoft Visual Studio\\VC98\\atl\\include;E:\\Microsoft Visual Studio\\VC98\\mfc\\include;E:\\Microsoft Visual Studio\\VC98\\include‘, ‘TEMP‘: ‘C:\\Users\\ASUS\\AppData\\Local\\Temp‘, ‘PROGRAMW6432‘: ‘C:\\Program Files‘, ‘PROCESSOR_REVISION‘: ‘4e03‘, ‘FPS_BROWSER_APP_PROFILE_STRING‘: ‘Internet Explorer‘, ‘CLASSPATH‘: ‘C:\\Program Files\\Java\\jdk1.7.0_17\\lib;C:\\Program Files\\Java\\jdk1.7.0_17\\lib\\dt.jar;C:\\Program Files\\Java\\jdk1.7.0_17\\lib\\tools.jar;C:\\Program Files\\Java\\jdk1.8.0\\lib;C:\\Program Files\\Java\\jdk1.7.0_17\\lib\\dt.jar;C:\\Program Files\\Java\\jdk1.7.0_17\\lib\\tools.jar‘, ‘FPS_BROWSER_USER_PROFILE_STRING‘: ‘Default‘, ‘HOMEPATH‘: ‘\\Users\\ASUS‘, ‘COMMONPROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)\\Common Files‘, ‘COMMONPROGRAMFILES‘: ‘C:\\Program Files\\Common Files‘})

os.makedirs(rE:\a\b\c\d)   #递归创建多级目录
os.removedirs(rE:\a\b\c\d)  #从后面逐级判断目录是否为空,若为空则删除
os.mkdir(rE:\a)  #一次只能创建一级目录
os.rmdir(rE:\a)  #一次只能删除一级目录,若为空删除,否则不删
os.remove(rE:\CLion\CLion 2017.2.2\build.txt)  #删除文件
1 print(os.listdir(r‘E:\CLion\CLion 2017.2.2))  #显示指定目录下的所有目录和文件,包括隐藏文件
2 os.rename(rE:\python练习\fib.py,rE:\python练习\fibnan1.py)  #重命名
3 print(os.stat(rE:\python练习))   #文件或目录状态信息

[‘bin‘, ‘help‘, ‘jre64‘, ‘lib‘, ‘license‘, ‘plugins‘]
os.stat_result(st_mode=16895, st_ino=18295873486193234, st_dev=2227732002, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1516964219, st_mtime=1516964219, st_ctime=1499412376)

1 os.system(ifconfig)  #运行shell命令
 1 print(os.path.abspath(__file__))  #绝对路径
 2 print(os.path.dirname(E:\python练习\python35学习\Day5\os_test.py)) #最后一个\之前的
 3 print(os.path.basename(E:\python练习\python35学习\Day5))   #最后一个\之后的
 4 print(os.path.split(E:/python练习/python35学习/Day5/os_test.py))  #将文件与目录拆分开,形成元组
 5 print(os.path.exists(E:/python练习/python35学习/Day5))    #判断文件或目录是否存在
 6 print(os.path.isabs(python练习/python35学习/Day5/os_test.py))   #判断是否是绝对路径
 7 print(os.path.isfile(E:/python练习/python35学习/Day5/os_test.py))   #判断是否是文件
 8 print(os.path.isdir(E:/python练习/python35学习/Day5/os_test.py))  #判断是否是目录
 9 print(os.path.join(rE:/python练习/python35学习/Day5/,rDay5.txt))   #路径连接
10 print(os.path.getatime(E:/python练习/python35学习/Day5/os_test.py))   #最后的存取时间access
11 print(os.path.getmtime(E:/python练习/python35学习/Day5/os_test.py))   #最后的修改时间modify

 

 








以上是关于python模块的主要内容,如果未能解决你的问题,请参考以下文章

Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段

python之模块和包

python中的模块

Python 常用模块学习

python之模块和包

Python 八Python模块