标准模块——OS模块
Microsoft Windows [版本 10.0.19041.208]
(c) 2020 Microsoft Corporation. 保留所有权利。
C:\Users\徐达>control
C:\Users\徐达>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> dir(os)
['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_AddedDllDirectory', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'add_dll_directory', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']
>>> os.environ
environ({
'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\徐达\\AppData\\Roaming', 'CLASSPATH': '.;C:\\Program Files\\Java\\jdk1.8.0_221\\lib\\dt.jar;C:\\Program Files\\Java\\jdk1.8.0_221\\lib\\tools.jar', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'COMPUTERNAME': 'LAPTOP-QI10CO2B', 'COMSPEC': 'C:\\Windows\\system32\\cmd.exe', 'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\徐达', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_221', 'LOCALAPPDATA': 'C:\\Users\\徐达\\AppData\\Local', 'LOGONSERVER': '\\\\LAPTOP-QI10CO2B', 'NUMBER_OF_PROCESSORS': '8', 'ONEDRIVE': 'C:\\Users\\徐达\\OneDrive', 'OS': 'Windows_NT', 'PATH': 'C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\Java\\jdk1.8.0_221\\bin;D:\\Python38\\Scripts\\;D:\\Python38\\;C:\\Users\\徐达\\AppData\\Local\\Microsoft\\WindowsApps;;D:\\PyCharm Community Edition 2020.2.2\\bin;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 165 Stepping 2, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': 'a502', 'PROGRAMDATA': 'C:\\ProgramData', 'PROGRAMFILES': 'C:\\Program Files', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'PROGRAMW6432': 'C:\\Program Files', 'PROMPT': '$P$G', 'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules', 'PUBLIC': 'C:\\Users\\Public', 'PYCHARM COMMUNITY EDITION': 'D:\\PyCharm Community Edition 2020.2.2\\bin;', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\Windows', 'TEMP': 'C:\\Users\\徐达\\AppData\\Local\\Temp', 'TMP': 'C:\\Users\\徐达\\AppData\\Local\\Temp', 'USERDOMAIN': 'LAPTOP-QI10CO2B', 'USERDOMAIN_ROAMINGPROFILE': 'LAPTOP-QI10CO2B', 'USERNAME': '徐达', 'USERPROFILE': 'C:\\Users\\徐达', 'WINDIR': 'C:\\Windows'})
>>>
>>> exit()
C:\Users\徐达>cls
C:\Users\徐达>calc
C:\Users\徐达>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('calc')
0
>>> os.system('C:\Users\徐达\Documents\liuyuboMLppt)
File "", line 1
os.system('C:\Users\徐达\Documents\liuyuboMLppt)
^
SyntaxError: EOL while scanning string literal
>>> os.system('C:\Users\徐达\Documents\liuyuboMLppt')
File "", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>> os.system('C:\Users\徐达\Documents\liuyuboMLppt')
File "", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>> os.system('C://Users//徐达//Document//liuyuboMLppt')
系统找不到指定的路径。
1
>>> os.sep
'\\'
>>> os.system('C:\\Users\\徐达\\Docunment\\liuyuboMLppt')
系统找不到指定的路径。
1
>>> os.system('C:\\Users\\徐达\\Docunments\\liuyuboMLppt')
File "", line 1
os.system('C:\\Users\\徐达\\Docunments\\liuyuboMLppt')
^
IndentationError: unexpected indent
>>> os.system('C:\\Users\\徐达\\Documents\\liuyuboMLppt')
File "", line 1
os.system('C:\\Users\\徐达\\Documents\\liuyuboMLppt')
^
IndentationError: unexpected indent
>>> os.system('C:\\Users\\徐达\\Documents\\liuyuboMLppt')
'C:\Users\徐达\Documents\liuyuboMLppt' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
1
>>> os.system('liuyuboMLppt')
'liuyuboMLppt' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
1
>>> os.getcwd()
'C:\\Users\\徐达'
>>> os.chdir('desktop')
>>> os.getcwd()
'C:\\Users\\徐达\\desktop'
>>> os.listdir()
['2020Python就业班 - 快捷方式.lnk', 'desktop.ini', 'eclipse - 快捷方式.lnk', 'Microsoft Edge.lnk', 'WPS Office.lnk', '大黄蜂云课堂播放器.lnk', '此电脑 -.lnk', '百度网盘.lnk']
>>> os.mkdir('hello')
>>> os.mkdir('hello')
Traceback (most recent call last):
File "", line 1, in <module>
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'hello'
>>> os.rename('hello','world')
>>> os.remdir('world')
Traceback (most recent call last):
File "", line 1, in <module>
AttributeError: module 'os' has no attribute 'remdir'
>>> os.rmdir('world')
>>> os.path
<module 'ntpath' from 'D:\\Python38\\lib\\ntpath.py'>
>>>
``>>> dir(os.path)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_abspath_fallback', '_get_bothseps', '_getfinalpathname', '_getfinalpathname_nonstrict', '_getfullpathname', '_getvolumepathname', '_nt_readlink', '_readlink_deep', 'abspath', 'altsep', 'basename', 'commonpath', 'commonprefix', 'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 'join', 'lexists', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath', 'relpath', 'samefile', 'sameopenfile', 'samestat', 'sep', 'split', 'splitdrive', 'splitext', 'stat', 'supports_unicode_filenames', 'sys']
>>> os.getcwd()
'C:\\Users\\徐达\\desktop'
>>> os.listdir()
['2020Python就业班 - 快捷方式.lnk', 'desktop.ini', 'eclipse - 快捷方式.lnk', 'Microsoft Edge.lnk', 'WPS Office.lnk', '大黄蜂云课堂播放器.lnk', '此电脑 -.lnk', '百度网盘.lnk']
>>> os.path.isdir('大黄蜂云课堂播放器.lnk')
False
>>> os.path.isdir('2020Python就业班 - 快捷方式.lnk')
File "", line 1
os.path.isdir('2020Python就业班 - 快捷方式.lnk')
^
IndentationError: unexpected indent
>>> os.path.isdir('2020Python就业班 - 快捷方式.lnk')
False
>>> os.listdir()
['2020Python就业班 - 快捷方式.lnk', 'desktop.ini', 'eclipse - 快捷方式.lnk', 'Microsoft Edge.lnk', 'WPS Office.lnk', '大黄蜂云课堂播放器.lnk', '新建文件夹', '此电脑 -.lnk', '百度网盘.lnk']
>>> os.path.isdir( '新建文件夹')
True
>>> os.path.isfile( '新建文件夹')
False
>>> os.path.exists('you')
False
>>> os.path.exists( '新建文件夹')
True
>>> f="C:\\Users\\徐达\\desktop\\desktop.ini"
>>> os.path.exist(f)
Traceback (most recent call last):
File "", line 1, in <module>
AttributeError: module 'ntpath' has no attribute 'exist'
>>> os.path.exists(f)
File "", line 1
os.path.exists(f)
^
IndentationError: unexpected indent
>>> os.path.exists(f)
True
>>> os.path.iffile(f)
Traceback (most recent call last):
File "", line 1, in <module>
AttributeError: module 'ntpath' has no attribute 'iffile'
>>> os.path.isfile(f)
File "", line 1
os.path.isfile(f)
^
SyntaxError: invalid character in identifier
>>> os.path.isfile(f)
True
>>> os.path.dirname(f)
'C:\\Users\\徐达\\desktop'
>>> os.path.split(f)
('C:\\Users\\徐达\\desktop', 'desktop.ini')
>>> os.path.basename(f)
'desktop.ini'
>>> name = "desktop.xxx.ini"
>>> name.split('.')
['desktop', 'xxx', 'ini']
>>> os.path.splitext(f)
('C:\\Users\\徐达\\desktop\\desktop', '.ini')
>>> os.path.join('c:\\desktop','a','b','c')
'c:\\desktop\\a\\b\\c'
>>> f2 = os.path.join('c:\\desktop','a','b','c')
>>> os.makedirs(f2)
```python
在这里插入代码片
datetime模块之时间转换
from datetime import datetime
import time
print(dir(datetime))
now_time = datetime.now()
print("now:{0}".format(now_time))
print('now day:{0}'.format(now_time.date()))
print('now time:{0}'.format(now_time.time()))
print('now days:{}'.format(datetime.today()))
print('year:{}'.format((now_time.year)))
print('month:{}'.format(now_time.month))
print('day:{}'.format(now_time.day))
print(dir(now_time))
print('microsecond:{}'.format(now_time.microsecond))
print(now_time.min)
print(now_time.isoweekday)
print('-----------------------------------------------------------------')
print(time.time())
from datetime import datetime, date, time, timedelta
d = datetime(2020, 10, 30, 14, 5)
print(d)
d2 = date(2019, 3, 23)
print(d2)
t = time(9, 0)
print(t)
print('________________________________________')
ds = '2018-10-12 13:42:09'
ds_t = datetime.strptime(ds, '%Y-%m-%d %H:%M:%S')
print(ds_t)
print(type(ds_t))
print(ds_t.year)
print(ds_t.month)
print(ds_t.day)
print(ds_t.time())
print(ds_t.second)
print("-----------------------------------------------")
n = datetime.now()
n_str = n.strftime('%Y-%m-%d %H:%M:%S')
print(n)
print(type(n))
print(n_str)
print(type(n_str))
print('________________________________________________')
n = datetime.now()
n_next = n + timedelta(days = 10,hours=25, seconds=2)
print(n_next)
d1 = datetime(2018 , 10 , 15)
d2 = datetime(2020 , 10 , 2)
rest = d2 - d1
print(rest)
print(dir(rest))
print(type(rest))
print(rest.days)
rest2 = d1 - d2
print(rest2)
第三方模块
- Django 一个自带电池的web开发框架
- Flask 简单易用,快速上手的微型web开发框架
- mysqlclient 使用python操作mysql数据的工具库
安装flask
- 登录:pypi查询
- cmd pip install Flask
编程练习
from datetime import datetime
import time
date_time = datetime(2019 , 10 , 10 , 8 , 10 ,0)
time.sleep(1)
date = datetime(2019 , 11 , 11)
print(date)
print(dir(time))
from datetime import datetime
import time
date_time = datetime(2019 , 10 , 10 , 8 , 10 ,0)
time.sleep(1)
date = datetime(2019 , 11 , 11)
print(date)
print(dir(time))
str_ = '2019-09-10 8:10:56'
str_date = datetime.strptime(str_,'%Y-%m-%d %H:%M:%S')
print(str_date)
from datetime import datetime , timedelta
now_ = datetime.now()
print(now_)
now_before = now_ - timedelta(3 , 36 , 12)
print(now_before)
now_after = now_ + timedelta(10)
print(now_after)