Lecture 30 and 31 of little turtle python -- Notes and exercises of file system / module

1.OS module: operation system
How to use the os module:

2.os.path module:
How to use the os.path module function:

os.path.basename('e:\\camera12\\a.jpg')
'a.jpg'
os.path.join('C:\\','A','B','C')
'C:\\A\\B\\C'
os.path.split('c:\\camera\\a.jpg')
('c:\\camera', 'a.jpg')
os.path.splitext('a.tif')
('a', '.tif')

3.pickle module

import pickle
malist=['123','other']
pickle_file=open('mylist.pkl','wb')#Write binary
pickle.dump(malist,pickle_file)#Save list in file
//Read:
pickle_file=open('mylist.pkl','rb')#Read binary
malist2=pickle.load(pickle_file)
print(malist2)
['123', 'other']

30 exercises and answers
0. Write a program to count the number of files of each file type in the current directory:

import os
import os.path 

count_txt=0
count_tif=0
count_else=0
path1=os.getcwd()#Get current path
print(path1)
file_list=os.listdir(path1)#Generate a list of file names in a folder
for each in file_list:
  file_tuple=os.path.splitext(each)#Separate file name from format
  if file_tuple[1]=='.txt':
    count_txt+=1
  else:
    if file_tuple[1]=='.tif':
      count_tif+=1
    else:
      count_else+=1

print('txt The format is%d individual'%count_txt)
print('tif The format is%d individual',count_tif)
print('Other formats are%d individual',count_else)

1. Write a program to calculate the size of all files in the current folder

import os
import os.path 

path1=os.getcwd()#Get current path
file_list=os.listdir(path1)#Generate a list of file names in a folder
for each in file_list:
  size=os.path.getsize(each)#getsize (file) get File Bytes
  print(each+'['+str(size)+']')

2. Write a program. The user enters the file name and the path to start the search. Search whether the modified file exists. If a folder is encountered, enter the folder to continue the search,

import os
import os.path 

def search_file(path_name,filename):
  os.chdir(path_name)#Modify the path to the top path

  for each_file in os.listdir(os.curdir):#Generate a list of files under the top path
    if each_file==filename:#Determine whether it is the target file
      print(os.getcwd()+os.sep+each_file)#If output path
    if os.path.isdir(each_file):#Determine whether the file is a folder / directory
      search_file(each_file,filename)#Go to this folder
      os.chdir(os.pardir)#If not, return to the previous menu


path_name=input('Please enter top folder:')
filename=input('Please enter the name of the file you want to search:')
search_file(path_name,filename)

3. Write a program, the user input the path to start the search, find all the video format files (. mp4,. rmvb,. avi format) under the path, and create a txt file to store all the found paths.

import os
import os.path 

file1=open('path.txt','a')#Create a file save path
def search_save(path_name):

  os.chdir(path_name)

  for each_file in os.listdir(os.curdir):#Determine whether a file is a folder
    if os.path.isdir(each_file):#If it's folder entry and recursion
      search_save(each_file)
      os.chdir(os.pardir)#Then return to the previous directory
    else:
      tuple_file=os.path.splitext(each_file)#Split name and format
      if tuple_file[1]=='.mp4'or #If the format is the following four tuple file [1] = '. RMVB' or tuple file [1] = '. Avi':
        #vidio_list.append(os.getcwd()+os.sep+each_file)
        file1.write(os.getcwd()+os.sep+each_file+'\n')#Write to txt file
path_name=input('Please enter top folder:')
vidio_list=[]
search_save(path_name)
file1.close()

Posted on Sat, 02 May 2020 08:31:11 -0700 by rroc