首页 > 其他分享 >Chapter 10

Chapter 10

时间:2022-12-14 19:12:33浏览次数:43  
标签:Chapter 10 name file print path message os

Antmanor_message

print("\n","="*10,"蚂蚁庄园的动态","="*10)
file = open('message_txt','w')
print("\n 即将显示……\n")

Antmanor_message_

print("\n","="*10,"蚂蚁庄园的动态","="*10)
file = open('message_txt','w')
print("\n 即将显示……\n")
file.write(" mingri的小鸡在你的庄园里呆了22min,吃了6g饲料后被你赶走了\n")
print('追加了一条动态\n')
file.close()

Antmanor_message_r

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

Antmanor_message_rl

print('\n','='*35,'蚂蚁庄园动态','='*35)
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','='*35,'over','='*35)

Fileinfo

import os 
fileinfo = os.stat('mr.png')
print('文件的完整路劲:',os.path.abspath('mr.png'))
print('索引号:',fileinfo.st_ino)
print('设备名',fileinfo.st_dev)
print('文件大小',fileinfo.st_size)
print('最后一次访问时间',fileinfo.st_atime)
print('最后一次修改时间',fileinfo.st_mtime)
print('最有一次状态变化时间',fileinfo.st_ctime)

Walk_list

import os 
path = 'c\\demo'
print("[",path,"]目录下包括的文件")
for root,dirs,files in os.walk(path,topdown=True):
    for name in dirs :
        print("1",os.path.join(root, name))
    for name in files :
        print('2',os.path.join(root, name))

 

Project

#实战1
import time
import os
#获取当前时间
time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
#获取当前路径
root = os.getcwd() 
print(root)
 
path =root + '\\'+ time_now
 
if not os.path.exists(path):
    os.makedirs(path)
    print('文件夹创建完成  '+path)
 
#实战2
import os
# 判断文件夹中是否有这些文件夹,没有的话就创建它
def initialize(file_dir):
    for al_name in os.listdir(file_dir):
        if (("." in al_name) == False):
            if not os.path.exists(os.path.join(file_dir, al_name).replace('\\', '/')):
                # print(os.path.join(file_dir, al_name).replace('\\', '/'))
                os.makedirs(os.path.join(file_dir, al_name).replace('\\', '/'))
                # 如果其中没有"1"的文件夹
            if not os.path.exists(os.path.join(file_dir, al_name, "1").replace('\\', '/')):
                os.makedirs(os.path.join(file_dir, al_name, "1").replace('\\', '/'))
            # 如果其中没有"2"的文件夹
            if not os.path.exists(os.path.join(file_dir, al_name, "2").replace('\\', '/')):
                os.makedirs(os.path.join(file_dir, al_name, "2").replace('\\', '/'))


initialize("./test")

 

标签:Chapter,10,name,file,print,path,message,os
From: https://www.cnblogs.com/Kyaria-code-test/p/16982979.html

相关文章