python模块

1:

>>> import random
>>> secret = random.randint(1,10)
>>> secret
9

2:os模块

>>> import os
>>> os.getcwd()
'C:\\Users\\littlelight\\AppData\\Local\\Programs\\Python\\Python39'
>>> os.chdir('E:\\')
>>> os.getcwd()
'E:\\'
>>> os.mkdir('E:\\A')
>>>os.remove('E:\\A\\test.txt')
>>> os.system('cmd')
>>> os.listdir(os.curdir)

3:os.path模块

>>> os.path.basename('E:\\A\\test,txt')
'test,txt'
>>> os.path.dirname('E:\\A\\test,txt')
'E:\\A'
>>> os.path.join('C','A','S')
'C\\A\\S'
>>> os.path.split('C:\\A\\B\\tect.txt')
('C:\\A\\B', 'tect.txt')
>>> os.path.getatime('D:\\python\\学习资料\\3record.txt')
1611714891.0460315
>>> import time
>>> time.gmtime(os.path.getatime('D:\\python\\学习资料\\3record.txt'))
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=27, tm_hour=2, tm_min=34, tm_sec=51, tm_wday=2, tm_yday=27, tm_isdst=0)
>>> time.localtime(os.path.getatime('D:\\python\\学习资料\\3record.txt'))
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=27, tm_hour=10, tm_min=34, tm_sec=51, tm_wday=2, tm_yday=27, tm_isdst=0)
>>> os.path.ismount('E:')
True

4:pickle模块

>>> import pickle
>>> my_list = [123,3.14, '小甲鱼',['another list']]
>>> pickle_file = open('my_list.pk1','wb')
>>> pickle.dump(my_list,pickle_file)
>>> pickle_file.close()
>>> pickle_file = open('my_list.pk1','rb')
>>> my_list2 = pickle.load(pickle_file)
>>> print(my_list2)
[123, 3.14, '小甲鱼', ['another list']]

5:统计文件夹内文件个数

#file_open = input('请输入要打开的文件夹及其目录:')
file_open =  'D:\\python\\学习资料'
import os
list1 = os.listdir(file_open)
suffixs = {}
for each in list1:
    if '.' in each:
        (name,suffix) = each.split('.',1)
    else:
        suffix = '文件夹'     
    if suffix not in suffixs:
        suffixs[suffix] = 1
    else:
        suffixs[suffix] += 1
for each in suffixs:
    print('该文件夹下共有类型为【.' +  each +'】的文件', suffixs[each],'个')

6:统计文件夹内文件大小

#path = input('请输入你要统计的路径:')
path = r'D:\python\实例'
import os
os.chdir(path)
list1 = os.listdir(path)
list2 = []
for each in list1:
    list2.append(os.path.getsize(each))
for each in range(len(list1)):
    print(list1[each] + '[' ,list2[each] ,'bytes]')

7:查找路径下文件

#path = input('请输入待查找的初始目录:')
#file = input('请输入需要查找的目标文件:')
path = r'D:\python'
file = 'something.txt'
import os
def seek(path):
    list1 = os.listdir(path)
    if file in list1:
        print(os.path.join(path,file))

    else:
        for each in list1:
            if '.' not in each:
                seek(os.path.join(path,each))
path1 = seek(path)

8:查找所有的txt和png文件并将路径记录在记事本中

#path = input('请输入待查找的初始目录:')
#file = input('请输入需要查找的目标文件:')
path = r'D:\python'
file = ['txt','png']
import os
def seek(path,list2):
    list1 = os.listdir(path)
    for each in list1:
        if '.' not in each:
            seek(os.path.join(path,each),list2)
        else:
            (name,suffix) = each.split('.',1)
            if suffix in file:              
                list2.append(os.path.join(path,each))
    return list2
list2 = seek(path,[])
f1 = open(r'D:\python\实例\24查找视频文件格式\24记录.txt','w')
for each in list2:
    f1.write(each)
    f1.write('\n')
f1.close()

9:统计路径下关键字个数

#file = input('请输入需要查找的关键字:')
keyword = '天'
path = r'D:\python\实例\20比较不同的文件'
import os
def seek(path):
    count1 = 0
    count2 = []
    count3 = 0
    count4 = 0
    list1 = os.listdir(path)
    for each in list1:
        if '.' not in each:
            seek(os.path.join(path,each))
        else:
            (name,suffix) = each.split('.',1)
            if suffix == 'txt':              
                f1 = open(os.path.join(path,each),'r')
                for each_line in f1:
                    count1 += 1
                    if keyword in each_line:
                        if count4 == 0:
                            count4 = 1
                            print('==============================================')
                            print('在文件【' + os.path.join(path,each) +'中找到关键字【' + keyword + '】')
                        for each_word in each_line:
                            count3 += 1
                            if each_word == keyword:
                                count2.append(count3)
                        print('关键字出现在第  %d  行' % count1 +'第',count2 ,'个位置')
                        count2 = []
                    count3 = 0
                count4 = 0
                count1 = 0           
list2 = seek(path)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值