程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python module + class

編輯:Python
1. Use recursion to list all files in the current directory ( Format requirements : Layered output )
requirement : least 4 Layer Directory : such as :
test:
-test.txt
-test_data.xls
-test1
-test1.txt
-test_data1.txt
-test2
-test2.txt
-test2_data.txt
-test3
-test3.txt
-test3_data.txt
import os
def traverse_file(path, arg=0):
for i in os.listdir(path): # Iterate over all directory names and file names under the specified directory level 
dir_path = os.path.join(path, i) # The full path 
if os.path.isdir(dir_path): # Determine whether it is a directory 
tree_str = "\t|" * arg + '-' * 4
print(tree_str + i)
traverse_file(dir_path, arg=arg + 1)
else:
tree_str = "\t|" * arg + '-' * 4
print(tree_str + i)
if __name__ == '__main__':
root_path = input(" Please enter the absolute path of the directory :")
print(root_path.split('\\')[-1] + '\\')
traverse_file(root_path)
D:\Python\python.exe F:/python/2021.8.16python/day05_work.py
Please enter the absolute path of the directory :D:\VMware
VMware\
----7za.exe
----adjperm.dll
----basichttp.dll
----DIFXAPI.dll
----diskLibWrapper.dll
----dispatcher.xml
----drvInst64.exe
----elevated.dll
----env
----hostd
|----coreLocale
| |----en
| | |----fault.vmsg
|----docroot
| |----client
| | |----clients.xml
| |----sdk
| | |----vimServiceVersions.xml
|----extensions
| |----hostdiag
| | |----extension.xml
| | |----locale
| | | |----en
| | | | |----enum.vmsg
| | | | |----event.vmsg
| | | | |----eventaux.vmsg
| | | | |----locmsg.vmsg
|----vimLocale
| |----defloc.vlcl
| |----en
| | |----auth.vmsg
| | |----default.vmsg
| | |----enum.vmsg
| | |----evc.vmsg
| | |----event.vmsg
| | |----eventaux.vmsg
| | |----fault.vmsg
| | |----gos.vmsg
| | |----host.vmsg
| | |----locmsg.vmsg
| | |----option.vmsg
| | |----perf.vmsg
| | |----question.vmsg
| | |----task.vmsg
| | |----vm.vmsg
| |----iso2psx.vlcl
| |----iso2win.vlcl
| |----isodata.vlcl
| |----loc2iso.vlcl
----ico
|----config.ico
|----generic.ico
|----import.ico
|----snapshot.ico
|----suspend.ico
|----vd.ico
----iconv.dll
----icudt44l.dat
----intl.dll
----isoimages_manifest.txt
----libcds.dll
----libcurl.dll
----libeay32.dll
----libxml2.dll
----linux.iso
----linuxPreGlibc25.iso
----messages
|----ja
| |----vmappsdk-ja.dll
| |----vmui-ja.dll
| |----vmware.vmsg
|----zh_CN
| |----vmappsdk-zh_CN.dll
| |----vmui-zh_CN.dll
| |----vmware.vmsg
----mkisofs.exe
----netadapter.inf
----netbridge.inf
----netuserif.inf
----netware.iso
----nfc-types.dll
----openssl.exe
----open_source_licenses.txt
----OVFTool
|----env
| |----defloc.vlcl
| |----en
| | |----action.vmsg
| | |----alarm.vmsg
| | |----auth.vmsg
| | |----cluster.vmsg
| | |----default.vmsg
| | |----enum.vmsg
| | |----evc.vmsg
| | |----event.vmsg
| | |----eventaux.vmsg
| | |----fault.vmsg
| | |----gos.vmsg
| | |----host.vmsg
| | |----locmsg.vmsg
| | |----option.vmsg
| | |----ovftool-warning.vmsg
| | |----ovftool.vmsg
| | |----perf.vmsg
| | |----question.vmsg
| | |----stask.vmsg
| | |----task.vmsg
| | |----vm.vmsg
| |----iso2psx.vlcl
| |----iso2win.vlcl
| |----isodata.vlcl
| |----loc2iso.vlcl
|----schemas
| |----DMTF
| | |----CIM_ResourceAllocationSettingData.xsd
| | |----CIM_VirtualSystemSettingData.xsd
| | |----common.xsd
| | |----dsp8023.xsd
| | |----dsp8027.xsd
| | |----xml.xsd
| |----vmware
| | |----ovf-vmware.xsd
| | |----ovfenv-vmware.xsd
|----ssleay32.dll
|----ssoclient.dll
|----vim-types.dll
|----vmacore.dll
|----vmomi.dll
|----vmware-eula.rtf
|----vmware.eula
|----xerces-c_3_1.dll
|----zlib1.dll
----ovftool_open_source_licenses.txt
----pcre.dll
----readme.txt
----Resources
|----isolinux.bin
|----pvscsi_Windows2003.flp
|----pvscsi_Windows8.flp
|----pvscsi_WindowsVista.flp
|----pvscsi_WindowsXP.flp
|----sample.flp
|----shared.lnk
|----storePwd.exe
|----storePwd.ini
|----suse.flp
|----unattend.cmd
|----unattend.flp
|----unattend.txt
|----unattend.xml
|----vmscsi.flp
----sigc-2.0.dll
----solaris.iso
----ssleay32.dll
----ssoclient.dll
----swagger.zip
----sysimgbase.dll
----tagExtractor.xml
----tools-upgraders
|----run_upgrader.sh
|----vmware-tools-upgrader-32
|----vmware-tools-upgrader-64
|----VMwareToolsUpgrader.exe
|----VMwareToolsUpgrader9x.exe
|----VMwareToolsUpgraderNT.exe
----TPClnRDP.dll
----TPClnt.dll
----TPClntdeu.dll
----TPClntjpn.dll
2. Give a score score, At random 8 A score =》 8 Sum of two output /8 = 80,
8 The distribution of scores ,score - 10 < score < score + 10
import random
score = 80
while True:
scores = random.sample(range(score - 10, score + 10), 8)
if sum(scores) / 8 == 80:
break
print(scores)
D:\Python\python.exe F:/python/2021.8.16python/day05_work.py
[70, 82, 79, 76, 86, 72, 87, 88]
Process finished with exit code 0
3. Define a class :Person
Class properties :type="student"
Object properties :name, age, gender
Method :print_info: Print the content : So-and-so is a good student.
Override... In a class :__new__ and __init__, And print __new__ and __init__ To display the called
Instantiate two objects : zhangsan, lisi And call the method :
class Person:
type = "student"
def __new__(cls, *args, **kwargs):
print("__new__")
return super(Person, cls).__new__(Person)
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
print("__init__")
def print_info(self):
print('{} is a good student'.format(self.name))
if __name__ == '__main__':
person = Person(" Zhang San ", 18, " male ")
person.print_info()
print(person.age, person.gender, Person.type, person.type)
D:\Python\python.exe F:/python/2021.8.16python/day05_work.py
__new__
__init__
Zhang San is a good student
18 male student student
Process finished with exit code 0

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved