文件及目录操作

print('\n','='*10,'蚂蚁庄园动态','='*10)
with open('message.txt','w') as file:
    pass
print('\n即将显示....\n')

print('\n','='*10,'蚂蚁庄园动态','='*10)
file=open('message.txt','w')
file.write('你使用了1张加速卡,小鸡撸起袖子开始双手吃饲料,进食速度大大加快。\n')
print('\n写入一条动态....\n')
file.close()


print('\n','='*25,'蚂蚁庄园动态','='*25,'\n')
with open('message.txt','r') as file:
    message=file.read()
    print(message)
print('\n','='*29,'over','='*29,'\n')

print('\n','='*35,'蚂蚁庄园动态','='*35,'\n')
with open('message.txt','r') as file:
    number=0
    while True:
        number+=1
        line=file.readline()
        if line =='':
            break
        print(number,line,end='\n')
print('\n','='*39,'over','='*39,'\n')

import os
path='E:\\pythontest'
print('[',path,']目录下包括的文件和目录:')
for root ,dirs,files in os.walk(path,topdown=True):
    for name in dirs:
        print('#',os.path.join(root,name))
    for name in files:
        print('#',os.path.join(root,name

fileinfo=os.stat('message.txt')
print('文件完整路径:',os.path.abspath('message.txt'))
print('索引号:',fileinfo.st_ino)
print('文件大小:',fileinfo.st_size,'字节')
print('最后一次访问时间:',fileinfo.st_atime)
print('最后一次修改时间:',fileinfo.st_mtime)
print('最后一次状态变化时间:',fileinfo.st_ctime)

import os
def formatTime(longtime):
    import time
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(longtime))
def formatByte(number):
    for (scale,label) in [(1024*1024*1024,'GB'),(1024*1024,'MB'),(1024,'KB')]:
        if number>=scale:
            return '%.2f %s' %(number*1.0/scale,label)
        elif number==1:
            return '1字节'
        else:
            byte='%.2f' %(number or 0)
    return (byte[:-3] if byte.endswith('.00') else byte)+' 字节'
if __name__=='__main__':
    fileinfo=os.stat('message.txt')
    print('文件完整路径:',os.path.abspath('message.txt'))
    print('索引号:',fileinfo.st_ino)
    print('文件大小:',fileinfo.st_size,'字节')
    print('最后一次访问时间:',fileinfo.st_atime)
    print('最后一次修改时间:',fileinfo.st_mtime)
print('最后一次状态变化时间:',fileinfo.st_ctime)

posted @ 2022-12-16 01:40  核变的思  阅读(47)  评论(0)    收藏  举报