未找到文件错误:[Errno 2]没有这样的文件或目录:'电影.csv'

roqulrg3  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(162)

我正在做一个电报机器人,它需要电影的数据,并把它放在一个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。需要帮助

yyyllmsg

yyyllmsg1#

如果您显式地定义这些参数,这会有帮助吗?

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

相关问题