Python review(2) -- About importing a module and dir-operation
2018-01-05 13:25
549 查看
1 Import a module before using it , if there is no module found , you have to install it , or , it doesn’t exist at all. When importing a module , renamed the module with grammer like this : import easygui as aa. Then you can call a function with aa.function().
2 Looping through something with for i in range(1,100)
3 Function in Module OS . Mostly concerned to file or dir.Baidu for more.Pay a attention to this >>> os.makedirs(r”.\a\b\c”).”r” make sure that the path avoid to tranlate String to Escape character.
4 Pickle is a useful and powerful module . You can save any format to a file and load any format from a file. Remember , import it first.
5 Try-catch ? No . He changed his name to Try-except . Of course Try-except-finally works . It works with “else” , too . Try-except-else.
6 I installed my own gui today , I will see how it works and what’s defferences between it and VB , tomorrow . Ok , I spelled a totally new word “deffefences”
>>> import easygui Traceback (most recent call last): File "<pyshell#105>", line 1, in <module> import easygui ModuleNotFoundError: No module named 'easygui' >>> import os >>> import os as aa >>> import gui Traceback (most recent call last): File "<pyshell#99>", line 1, in <module>
2 Looping through something with for i in range(1,100)
3 Function in Module OS . Mostly concerned to file or dir.Baidu for more.Pay a attention to this >>> os.makedirs(r”.\a\b\c”).”r” make sure that the path avoid to tranlate String to Escape character.
>>> import os >>> listdir(path = '.') >Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> listdir(path = '.') NameError: name 'listdir' is not defined >>> os.listdir(path = '.') ['boy.txt', 'chen', 'DLLs', 'Doc', 'girl', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'project', 'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 'Scripts', 'snow.txt', 'tcl', 'Tools', 'vcruntime140.dll'] >>> os.sep '\\' >>> os.linesep '\r\n' >>> os.curdir '.' >>> os.pardir '..' >>> os.name 'nt' >>> os.getcwd <built-in function getcwd> >>> os.getcwd() 'C:\\Users\\Chen\\AppData\\Local\\Programs\\Python\\Python36-32' >>> os.listdir("C:\\") ['$360Section', '$RECYCLE.BIN', '360SANDBOX', '80cf1df38b1cab2d1795f15a6b7fe5f3', 'APLoading.exe', 'AppData', 'apps', 'AV Bros', 'bootmgr', 'BOOTNXT', 'config.ini', 'CustomDll.dll', 'DBAR_Ver.txt', 'DELL', 'dell.sdr', 'Documents and Settings', 'Drivers', 'en-US', 'GameDownload', 'HaxLogs.txt', 'hiberfil.sys', 'HP Universal Print Driver', 'InstallConfig.ini', 'Intel', 'msdia80.dll', 'MSOCache', 'MWizard.exe', 'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Recovery', 'ServerConfig', 'snapshot', 'swapfile.sys', 'System Volume Information', 'Temp', 'temporarysoft', 'Users', 'VSSCap.exe', 'vstudio.exe', 'WCH.CN', 'Windows', '迅雷下载'] >>> os.mkdir("test") >>> os.listdir() ['boy.txt', 'chen', 'DLLs', 'Doc', 'girl', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'project', 'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 'Scripts', 'snow.txt', 'tcl'< a8f4 /span>, 'test', 'Tools', 'vcruntime140.dll'] >>> os.makedirs(".\a\b\c") Traceback (most recent call last): File "<pyshell#26>", line 1, in <module> os.makedirs(".\a\b\c") File "C:\Users\Chen\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 210, in makedirs makedirs(head, mode, exist_ok) File "C:\Users\Chen\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 220, in makedirs mkdir(name, mode) OSError: [WinError 123] 文件名、目录名或卷标语法不正确。: '.\x07\x08' >>> os.makedirs(r".\a\b\c") >>> os.listdir(".\a") Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> os.listdir(".\a") >>> os.listdir(r".\a") ['b'] >>> for each in os.walk('.'): print(each) ('.', ['a', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'project', 'Scripts', 'tcl', 'test', 'Tools'], ['boy.txt', 'chen', 'girl', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 'snow.txt', 'vcruntime140.dll']) ('.\\a', ['b'], []) ('.\\a\\b', ['c'], []) ('.\\a\\b\\c', [], []) ('.\\DLLs', [], ['py.ico', 'pyc.ico', 'pyd.ico', 'pyexpat.pyd', 'python_lib.cat', 'python_tools.cat', 'select.pyd', 'sqlite3.dll', 'tcl86t.dll', 'tk86t.dll', 'unicodedata.pyd', 'winsound.pyd', '_asyncio.pyd', '_bz2.pyd', '_ctypes.pyd', '_ctypes_test.pyd', '_decimal.pyd', '_elementtree.pyd', '_hashlib.pyd', '_lzma.pyd', '_msi.pyd', '_multiprocessing.pyd', '_overlapped.pyd', '_socket.pyd', '_sqlite3.pyd', '_ssl.pyd', '_testbuffer.pyd', '_testcapi.pyd', '_testconsole.pyd', '_testimportmultiple.pyd', '_testmultiphase.pyd', '_tkinter.pyd']) ('.\\Doc', [], ['python364.chm'])
4 Pickle is a useful and powerful module . You can save any format to a file and load any format from a file. Remember , import it first.
>>> import pickle >>> my_list = [123,3.14159,'陈学俊',['another list']] >>>> pickle_file = open("my_list.pkl","wb") >>> pickle.dump(my_list,pickle_file) >>> pickle_file.close() >>> f = open("my_list.pkl") >>>> my_list = pickle.load(f) >>> print(my_list) [123, 3.14159, '陈学俊', ['another list']]
5 Try-catch ? No . He changed his name to Try-except . Of course Try-except-finally works . It works with “else” , too . Try-except-else.
>>> try: sum = 1 + 'w' f = open("hello.txt") print(f.read()) f.close() except OSError as reason: print("文件打开出错,错误原因是:"+str(reason)) except TypeError as reason1: print("加法有误,错误原因是:"+str(reason1)) 加法有误,错误原因是:unsupported operand type(s) for +: 'int' and 'str' >>> try: sum = 1 + 'w' f = open("hello.txt") print(f.read()) f.close() except OSError as reason: print("文件打开出错,错误原因是:"+str(reason)) except: print("错误") 错误 >>> raise OSError('操作系统有误') Traceback (most recent call last): File "<pyshell#95>", line 1, in <module> raise OSError('操作系统有误') OSError: 操作系统有误
6 I installed my own gui today , I will see how it works and what’s defferences between it and VB , tomorrow . Ok , I spelled a totally new word “deffefences”
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import easygui as gui >>>
相关文章推荐
- About Module and Package in Python
- About Module and Package in Python
- Python 3.6 adds New secrets Module for Robust Account and Password Security
- About the Rebase and Bind operation in the production of software
- Operation about Excel with Python
- Python's datetime module, numpy's datetime64/timedelta64 and pandas' Timestamp/Timedelta objects
- about macros fs_initcall and module_init
- 30 Python Language Features and Tricks You May Not Know About
- psutil - A cross-platform process and system utilities module for Python - Google Project Hosting
- Python: import Module and Class
- Use Module and Function instead of Class in Python
- Importing Python Modules(import and from import, import as)
- 30 Python Language Features and Tricks You May Not Know About
- 解决java调用python的Cannot import site module and its dependencies: No module named site问题
- ROS:关于tf的探索(4)Learning about tf and time(Python)
- 30 Python Language Features and Tricks You May Not Know About
- greecket - a python not block socket module base the greenlet and thread - Google Project Hosting
- installation about python package and complier
- [Python]Shallow and Deep copy operation
- How to download and install setuptools module for Python