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

希望對你有幫助

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