FileNotFoundError: [Errno 2] No such file or directory: 'movies.csv'

问题描述 投票:0回答:1

我正在制作一个电报机器人,它获取电影数据并将其放入一个 csv 文件中,我稍后会提供该文件用于导出, 但在我的代码中,csv 文件没有被创建,如果我添加一个预先添加的 csv 文件,数据就没有被导出到它。 这是完整的代码

import os
import telebot
import requests
import json
import csv
from dotenv import load_dotenv
load_dotenv()
# TODO: 1.1 Get your environment variables 
bot_id = os.getenv('botkey')
my_key = os.getenv('keyy')
bot = telebot.TeleBot(bot_id)

@bot.message_handler(commands=['start', 'hello'])
def greet(message):
    global botRunning
    botRunning = True
    bot.reply_to(
        message, 'Hello there! I am a bot that will show mov information for you and export it in a CSV file.\n\n')
    
@bot.message_handler(commands=['stop', 'bye'])
def goodbye(message):
    global botRunning
    botRunning = False
    bot.reply_to(message, 'Bye!\nHave a good time')
    


@bot.message_handler(func=lambda message: botRunning, commands=['help'])
def helpProvider(message):
    bot.reply_to(message, '1.0 You can use \"/movie MOVIE_NAME\" command to get the details of a particular mov. For eg: \"/movie The Shawshank Redemption\"\n\n2.0. You can use \"/export\" command to export all the mov data in CSV format.\n\n3.0. You can use \"/stop\" or the command \"/bye\" to stop the bot.')


@bot.message_handler(func=lambda message: botRunning, commands=['movie'])
def getMovie(message):
    bot.reply_to(message, 'Getting movie info...')
    mov = message.text
    mov = mov.replace("/movie","")
    response = requests.get(f'http://www.omdbapi.com/?apikey=a13fc218&t={mov}')
    mov_data=response.json()
    print(json.dumps(mov_data,indent = 4))
    bot.reply_to(message,f"{mov_data['Poster']} \nMovie Name: {mov_data['Title']} \nYear: {mov_data['Year']} \nReleased: {mov_data['Released']} \nImdb Rating: {mov_data['imdbRating']}")
    bot.send_photo(message,{mov_data['Poster']})
    with open('movies.csv','a',encoding='UTF8',newline='') as f:
        writer = csv.writer(f)
        writer.writerow([mov_data['Title'],mov_data['Year'],mov_data['Released'],mov_data['imdbRating']])


  
@bot.message_handler(func=lambda message: botRunning, commands=['export'])
def getList(message):
    bot.reply_to(message, 'Generating file...')
    chat_id = message.chat.id 
    print()
    mov_data=open('movies.csv','rb')
    bot.send_document(chat_id,mov_data)
    

@bot.message_handler(func=lambda message: botRunning)
def default(message):
    bot.reply_to(message, 'I did not understand '+'\N{confused face}')
    
bot.infinity_polling()

是的,我试图使创建文件的位置非常具体,但也没有用。 我正在研究 macbook。 需要帮助

python csv telegram export-to-csv python-telegram-bot
1个回答
0
投票

如果您明确定义该参数,这有帮助吗?

def getList(message):
    with open('movies.csv','rb') as movie_csv_file:
        bot.send_document(
            chat_id=message.chat.id ,
            document=movie_csv_file
            filename="all_movies.csv"
        )
© www.soinside.com 2019 - 2024. All rights reserved.