Python检查当前目录下是否存在所需的文件夹或文档,若不存在,则需要自动生成

实现的功能是:当前目录下是否存在所需的文件夹或文档,若不存在,则需要自动生成

import os

# 检查相应的文件夹是否存在
def mkdir():

    file_path = os.getcwd() #获得当前工作目录

    if os.path.exists(file_path + "\输入文件"):
        print("输入文件(文件夹)存在")
    else:
        os.makedirs(file_path + "\输入文件")


# 判断相应文本是否在对应的文件夹下
def mkfile():
    file_path = os.getcwd()  # 获得当前工作目录

    if os.path.exists(file_path + "\停用词" + "\stopword.txt"):
        print("stopword.txt 存在")
    else:
        print("新建 stopword.txt")
        open(file_path + "\停用词" + "\stopword" + ".txt", "w")


if __name__ == "__main__":
    mkdir()
    mkfile()

希望对你有帮助

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章