--Object: A set of variables and related functions ⇒ Easy to program because it is a set --Class: The name of the object template --Constructor ... Initial value of template * Init is surrounded by two underscores, and self is added to the argument. --Method: A function defined in the class. Use this function to run the class
--Instance: A base that calls the constructor of the class (template) and actually operates it. --Member: Refer to the class (template) argument
--Object: A set of variables related to a function that sorts by file name in descending order and measures the size of a file. --Class: FileControl --Constructor ... Declare a dir variable to store the file reference as an argument --Method: Declare make_r_list to sort by file name in descending order and size_check to get the file size.
--Instance: Create an instance path by calling FileControl with a file reference as an argument. --Member: Check the stored value with dir that stores the argument in the instance path.
"We called the FileControl class to create a path instance, sorted the files in descending order with the make_r_list method, and measured the size of the file with the size_check method."
python
import os
import pprint
#Define FiileControl class
class FileControl:
#Define constructor
def __init__(self,dir):
self.dir=dir
#Method to sort by file name in descending order
def make_r_list(self):
dirs=[]
for n in os.listdir(self.dir):
#↓ Add the full path to the array because the full path is required to measure the file size.
dirs.append(self.dir+n)
dirs.sort(reverse=True)
return dirs
#Method to get file size
def size_check(self,fname):
s_var=os.path.getsize(fname)
s_var=s_var/(1024*100)
return s_var
#↑↑↑↑↑ Everything above is defined.
#↓ ↓ ↓ ↓ ↓ From here on down is the actual operation.
#Call FileControl to create a path (instance)
path=FileControl('c:/users/user/desktop/PDF/')
#Confirmation of members
print(path.dir)
#Sort by file name in descending order and return a list, make_r_run list
list=path.make_r_list()
#Get the file size by reading the files one by one from the list variable size_execute check method
for f in list:
#Because format can be displayed at the same time without converting the integer type
pprint.pprint('file name:{0}--file size_{1:.1f}KB'.format(f,path.size_check(f)))
Execution result. The file name and size (KB conversion) are displayed.
Is it like attaching tires (methods) and pedals (methods) to a bicycle frame (class) and giving air (arguments) to the tires and propulsion (arguments) to the pedals? Eventually I saw somewhere that every language had to remember arrays and classes, so I wrote it.
Recommended Posts