首页 > 其他分享 >文件及目录操作

文件及目录操作

时间:2022-12-16 01:55:15浏览次数:43  
标签:文件 number 目录 print 操作 fileinfo st os message

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)

标签:文件,number,目录,print,操作,fileinfo,st,os,message
From: https://www.cnblogs.com/whc123/p/16986381.html

相关文章

  • 使用python操作数据库
    importsqlite3conn=sqlite3.connect('mrsoft.db')cursor=conn.cursor()cursor.execute('createtableuser(idint(10)primarykeynamevarchar(20))')cursor.clos......
  • nodejs命令行内部中执行js文件(编辑模式)
    我想在这种状态中执行js文件,用来调试一些代码,没找到现成的函数,只能是读取文件然后eval了vardstr=''fs.readFile('./run.js','utf8',function(err,datastr) { if(e......
  • docker安装minio OSS文件存储服务
    查看minio的docker版本使用docker命令查看minio的版本dockersearchminio拉取镜像dockerpullminio/minio创建目录:一个用来存放配置,一个用来存储上传文件的目录......
  • uniapp本地文件读写操作
    说明主要是封装官方文档里面的官方文档地址:https://www.html5plus.org/doc/zh_cn/io.html创建或打开文件//fileName目录路径dirEntry之前打开过的目录(没有则不填写......
  • 计算机和操作系统基础知识
    计算机和操作系统基础知识  1、操作系统(OperatingSystem,简称OS),是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内核与基石 操作系统需要处理如管理与配置......
  • orm查询操作 以及多表的查询
    今日内容详细ORM执行SQL语句有时候ORM的操作效率可能偏低我们是可以自己编写SQL......
  • Django之ORM相关操作
    Django之ORM相关操作目录Django之ORM相关操作ORM执行SQL语句神奇的双下划线查询ORM外键字段的创建外键字段相关操作ORM跨表查询基于对象的跨表查询基于上下划线的跨表查询......
  • C++学习---cstdio的源码学习分析05-打开文件函数fopen
    cstdio中的文件访问函数stdio.h中定义了一系列文件访问函数(fopen,fclose,fflush,freopen,setbuf,setvbuf),接下来我们一起来分析一下fopen对应的源码实现。fopen:打开文件fclose:关......
  • Linux:通过命令查找日志文件中的某字段
    工作中有用到,做个记录。1.查询某字段,显示行号:cat-nfile_name|grep'查找字段'[root@ZWZF-CWY-LZY-12CWY]#cat-nnohup.out|grep'JVM'104452022-12-151......
  • ORM执行SQL语句,神奇的双下划线查询,ORM外键字段的创建,外键字段数据的操作,多表查询
    ORM执行SQL语句,神奇的双下划线查询,ORM外键字段的创建,外键字段数据的操作,多表查询一、ORM执行SQL语句有时候ORM的操作效率较低,我们是可以自己来编写SQL语句的方式一:......