首页 > 编程语言 >python:File

python:File

时间:2023-07-09 13:55:13浏览次数:36  
标签:python curpath nowfoloder newname File print path os

 

def getFile(prefixletter:str,ok:bool):
    """
    文件名添加后缀
    :param prefixletter:
    :param ok:  True 增加后缀,否则为删除
    :return:
    """
    try:
        nowfoloder = os.getcwd()
        curpath=os.path.join(nowfoloder,r"static")
        fiellist=os.listdir(curpath)
        n=1
        os.chdir(curpath)
        for i in fiellist:
            print("list", i)
            if ok==False:
                newname=i.split("_")
            else:
                newname = i.split(".")
            print(newname[0]+'_'+prefixletter+"."+newname[1])
            #

            if os.path.isfile(i):
                if ok==True:
                    os.rename(i, newname[0] + '_' + prefixletter + "." + newname[1])
                else:
                    os.rename(i, newname[0] + ".text")
            n+=1
    except Exception as ex:
        print(ex)

    finally:
        pass




getFile("d", False)
"""
try:
    nowfoloder=os.getcwd()
    fillist=os.listdir(nowfoloder)
    print(fillist)
    curpath=os.path.join(nowfoloder,r"static")
    print(curpath)
    os.chdir(curpath)
    for f in os.listdir():
        print(f)
    if(not os.path.isdir("images")):
        os.mkdir("images")
    if (not os.path.isdir("test")):
        os.mkdir("test")

    for f in os.listdir():
        print(f)
    if os.path.isdir("test"):
        curpath = os.path.join(nowfoloder, r"static")
        os.chdir(curpath)
        print(os.getcwd())
        if os.path.isdir("test"):
            os.rmdir(os.path.join(curpath,"test"))
except Exception as ex:
    print(ex)

finally:
    pass

"""

  

标签:python,curpath,nowfoloder,newname,File,print,path,os
From: https://www.cnblogs.com/geovindu/p/17538659.html

相关文章

  • python - 函数(二)
    4.传递列表defgreet_users(names):"""向列表中的每位用户发出问候。"""fornameinnames:msg=f"Hello,{name.title()}"print(msg)usernames=['hanks','jackson','jimmy']......
  • 中转转运运输问题——Python实现
    在供应链中,中转运输是一项关键活动,用于解决商品在运输过程中的各种限制和需求。商业部门承担中转运输的责任,组织商品的再次发运,以确保顺利的货物流动。中转运输在供应链中具有重要作用,主要原因如下:物流条件限制:由于运输条件的限制,商品可能无法直接一次性运送到目的地。这可能涉......
  • python - 函数(一)
    1.示例defgreet_user():#函数定义"""显示简单的问候语"""#文档字符串,描述了函数的功能。Python基于此生成有关函数的文档print("Hello!")greet_user()1.1参数defgreet_user(username):"""显示简单的问候语"&......
  • python: generate and decode QrCode
     #encoding:utf-8#-*-coding:UTF-8-*-#版权所有2023©涂聚文有限公司#许可信息查看:#描述:#Author:geovindu,GeovinDu涂聚文.#IDE:PyCharm2023.1python311#Datetime:2023/7/511:08#User:geovindu#Product:UI#Project......
  • python笔记1.2
    基本输入函数input的应用name=input('请输入您的姓名')print('您的姓名为:'+name)num=int(input('请输入您的幸运数字'))#print('您的幸运数字为:'+num)#字符串和整数无法运算print('您的幸运数字为:',num)#正常返回num单行注释#正常返回num多行注释'''版权所有......
  • python笔记:第四章使用字典
    1.1概述说白了就是键值对的映射关系不会丢失数据本身关联的结构,但不关注数据的顺序是一种可变类型格式:dic={键:值,键:值}键的类型:字典的键可以是任何不可变的类型,如浮点数,字符串,元组1.2函数dict可以从其他映射或键值对创建字典items=[('name','Gumby'),('ag......
  • python笔记1.1
    ASCII码使用print输出中文Unicode编码:print(ord("天"))#使用ord()查询“天”的Unicode编码为22825print("\u5929")#22825的十六进制为5929返回值为“天” 使用print()将内容输出到文件fp=open("note.txt","w")#打开文件,w——writeprint("北京欢迎你",file=fp)#输出......
  • Python潮流周刊#10:Twitter 的强敌 Threads 是用 Python 开发的!
    你好,我是猫哥。这里每周分享优质的Python及通用技术内容,大部分为英文,已在小标题注明。(标题取自其中一则分享,不代表全部内容都是该主题,特此声明。)首发于我的博客:https://pythoncat.top/posts/2023-07-08-weekly周刊已开通Telegram频道,欢迎关注:https://t.me/pythontrendingwee......
  • python - while循环(二)
    使用while处理列表和字典1.在列表之间移动元素在for循环遍历列表时,若修改列表元素会导致Python难以跟踪元素。unconfirmed_users=['alice','brian','candace']confirmed_users=[]forunconfirmed_userinunconfirmed_users:unconfirmed_users.remove(unconfirm......
  • Python | 使用try-except导包
    导包的时候我们可能会遇到这样的代码:try:from.hugmodelimportHugModelexceptException:pass这段代码的作用是尝试导入名为HugModel的模块,如果导入失败则不做任何操作,而是直接跳过异常。其中.表示当前包,也就是相对导入方式。这种写法通常用于可选的依赖项或......