• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

텔레그램 봇 강의 중 질문입니다.

20.02.29 21:21 작성 조회수 559

1

안녕하세요~ 강의 잘 듣고 있습니다~

텔레그램 봇 만들기 2번째 심화 강의 중 (강의 시작 9분 정도),

텔레그램에 '/dir' 을 입력하면 

터미널 창에 module 'ntpath' has no attribute 'exist' 라는 메시지가 출력됩니다.

스택오버플로우로 검색해 봐도 어떤 문제인지 찾지를 못했어요;;

파이썬은 3버전 이상입니다.

어떤 문제일까요;;; 

import os
import telepot
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


TELEGRAM_TOKEN = "1112174312:AAEyoQlv66VJuO2NyqLWHvjg7tAE4JMdavQ"


def get_dir_list(dir):
    str_list = ""
    if os.path.exists(dir):
        file_list = os.listdir(dir)
        file_list.sort()
        for f in file_list:
            full_path = os.path.join(dir, f)
            if os.path.isdir(full_path):
                f = "[" + f + "]"
            str_list += f
            str_list += "\n"
    str_list.strip()
    return str_list


def handler(msg):
    content_type, chat_type, chat_id, msg_date, msg_id = telepot.glance(
        msg, long=True)

    print(msg)

    # /dir c:\\test
    if content_type == "text":
       # bot.sendMessage(chat_id, "[반사] {}".format(msg["text"]))
        str_message = msg["text"]
        if str_message[0:1] == "/":
            args = str_message.split(" ")
            command = args[0]
            del args[0]

            if command == "/dir":
                filepath = " ".join(args)
                if filepath.strip() == "":
                    bot.sendMessage(chat_id, "/dir [대상폴더]로 입력해 주세요.")
                else:
                    filelist = get_dir_list(filepath)
                    bot.sendMessage(chat_id, filelist)


bot = telepot.Bot(TELEGRAM_TOKEN)
bot.message_loop(handler, run_forever=True)

답변 2

·

답변을 작성해보세요.

1

확실치는 않지만 파이썬 라이브러리가 꼬인거 같은 느낌입니다만..

import os
print(os.path)

위와 같이 os.path를 화면에 출력해서 출력되는 경로가 현재 사용중인 파이썬 폴더와 일치 하는지 확인해보시기 바랍니다.

<module 'ntpath' from 'C:\\Python\\lib\\ntpath.py'>

제 컴퓨터에서는 위과 같이 결과가 출력됩니다.

0

웨스님의 프로필

웨스

질문자

2020.03.02

확인하겠습니다.

감사합니다~