2.运行代码

#程序员如何平衡日常编码工作与提升式学习?#


import os
import time

"""
Success is where preparation and opportunity meet.

1.封装起来的好处:我们不必知道其内部的具体信息,知道参数即可
    内容:os.getcwd()
         os.listdir(path)
         os.chdir(path)
"""

######################方法的定义###################################
#1.获取当前路径
def getPresentPath():
    return os.getcwd()

#2.获取该目录下所有文件名称
def getPresentPathNames(path):
    return os.listdir(path) # 返回的是一个列表形式

# 3.拼接要访问的文件路径
def concatPath(presentDir,fileName):
    return presentDir +"\\" +str(fileName)

# 4. 用open保存内容,追加内容的形式

def saveRuntDataTime(path,data):
    with open(path, 'a+') as file:
        file.write(data+os.linesep) # os.linesep 是用来换行的
        print('内容保存成功!')

##########################################################


# 1.获取当前路径 https://www.runoob.com/python/os-chdir.html
present_path = getPresentPath()

# 2.获取当前路径下的所有文件名称
all_name = getPresentPathNames(present_path)
print(all_name)
print(present_path)

# 3. 拼接路径,转化成一个可以使用的目录

folder_path = concatPath(present_path,all_name[0])
print("当前拼接好的路径:"+folder_path)

# 4.转变工作目录
os.chdir(folder_path)
print("当前工作的路径:"+getPresentPath())

# 5.接下来就可以批量的操作当前文档下的所有文件:如用pandas处理excel(表格)或 用with open
import pandas as pd
# 5.1
# data = pd.read_excel('./') # 表格所在位置——名称

# 5.2 添加
names = getPresentPathNames(getPresentPath())
print(names)



# 6.时间的操作,记录每次运行时间:
#saveRuntDataTime
print('1.输出最近访问时间:'+str(os.path.getatime(names[0]) ) )  # 输出最近访问时间
visitDate = '2.输出最近访问时间:'+str(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(os.path.getatime(names[0]) )) )# 输出最近访问时间
print(visitDate)

# 7. 保存每次的运行时间 https://www.runoob.com/python/python-date-time.html
# 7.1 文件位置
visitFilePath = concatPath(getPresentPath(),names[0])
# 7.2 运行时间
runTime =  time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
saveRuntDataTime(visitFilePath,runTime)


































  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值