首页 > 其他分享 >10代码

10代码

时间:2022-11-03 16:00:38浏览次数:76  
标签:10 name 代码 file print path fileinfo os

实例01

print('\n','='*10,'蚂蚁庄园动态','='*10)

with open('message.txt','w') as file:

    pass

print('\n即将显示....\n')

 

实例02

print('\n','='*10,'蚂蚁庄园动态','='*10)

file=open('message.txt','w')

file.write('你使用了1张加速卡,小鸡撸起袖子开始双手吃饲料,进食速度大大加快。\n')

print('\n写入一条动态....\n')

file.close()

 

 

实例03

print('\n','='*25,'蚂蚁庄园动态','='*25,'\n')

with open('message.txt','r') as file:

    message=file.read()

    print(message)

print('\n','='*29,'over','='*29,'\n')

 

实例04‘

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')

实例05

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))

 

实例06

import os

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)

’实战1

import time

def formatTime():

    global name

    Time=time.strftime('%Y%m%d%H%M%S',time.localtime())

    name=Time+'.txt'

    with open(name,'a') as file:

        pass

if __name__=='__main__':

    a=int(input('请输入需要生成的文件数:'))

    for i in range(a):

        formatTime()

        print('file'+str(i+1)+':'+name)

        time.sleep(1)

print('生成文件成功!')

 

 

实战2

import os

 

path = r'E:\批量创建文件夹\\'

 

def mkdir(self):

    for i in range(a):

        file_num = str(i+1)

        file_name='文件夹'+file_num

        dir_name = path + file_name

        if file_name not in os.listdir(path):

            os.mkdir(dir_name)

            print(file_name,'创建成功!')

 

a=int(input('请输入需要生成的文件夹的个数:'))

mkdir(a)

 

 

标签:10,name,代码,file,print,path,fileinfo,os
From: https://www.cnblogs.com/crz11/p/16854702.html

相关文章

  • 微信小程序发布上传之后遇到代码质量未通过问题
     第一个问题:js代码压缩,我们只要在小程序开发者工具中,右上角详情这里点开,本地设置里把上传代码时自动压缩脚本文件勾上就可以了  第二个问题:启用组件按需注入的......
  • 收藏贴!Salesforce开发课程必看的10个Apex最佳实践
    Apex是一种强类型的,面向对象的编程语言,开发人员通过Apex表现业务逻辑来补充Salesforce平台所需的功能。Apex与Java很像,可以通过各种用户启动的事件来触发,例如记录更新,单击......
  • 安装Mariadb-10.6.10 需要升级CMake 3.0 以上版本
    下载wgethttps://cmake.org/files/v3.22/cmake-3.22.6.tar.gz安装tarzxvfcmake-3.22.6.tar.gzcdcmake-3.22.6./bootstrapgmakegmakeinstall注意安装完后......
  • 使用MVC实现登录注册功能(数据保存到数据库)详细讲解以及代码
    M:代表模型层,解决问题的功能具体实现。比如向数据库添加数据、查询数据V:代表视图,用户和机器的交互页面。用来展示信息(一般用html,js,css...)C:控制层,用来连接用户提交的操作......
  • Python实验报告——第10章 文件及目录操作
    实验报告【实验目的】 1.掌握Python自带的函数进行基本文件操作。2.掌握Python内置的os模块及其子模块os.path进行目录相关的操作。【实验条件】1.PC机或者远程编......
  • 代码随想录第二十三天 | 二叉树终章
    今天终于是二叉树的最后一章了,三道题,加油!669.修剪二叉搜索树classSolution{publicTreeNodetrimBST(TreeNoderoot,intlow,inthigh){if(root=......
  • 1049 数列的片段和
    题目:1049数列的片段和 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列{0.1,0.2,0.3,0.4},我们有(0.1)(0.1,0.2)(0.1,0.2,0.3)......
  • 10.内置函数
    截止到python3.9,一共有60多个内置函数,本篇对常用的函数进行分类罗列一下,对于文档请查看https://docs.python.org/zh-cn/3.9/library/functions.html1.数学运算abs(x):......
  • AIRIOT物联网低代码平台如何配置OPC UA驱动?
    AIRIOT物联网低代码平台支持驱动类型丰富,驱动包括但不限于通用驱动信息,无线驱动信息,行业驱动信息及各个厂商驱动。驱动稳定性强,经受住各个大型项目考验,持续稳定运行。A......
  • app直播源代码,Extjs Grid自动换行
    app直播源代码,ExtjsGrid自动换行定义列的时候加个renderer,例 Js代码      {      header:'序号',      dataIndex:'ind',    ......