Python review(2) -- About importing a module and dir-operation

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().

>>> 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', '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
>>> 

猜你喜欢

转载自blog.csdn.net/qq_36880027/article/details/78980221
今日推荐