Home > Net >  Google colab can't read csv file though I entered correct path
Google colab can't read csv file though I entered correct path

Time:11-10

I tried to get genres of songs in regional-us-daily-latest, and output genres and other datas as csv file. But colab said,

FileNotFoundError: [Errno 2] No such file or directory: 'regional-us-daily-latest.csv'

I mounted My Drive, but still didn't work. Could you shed some light on this?

!pip3 install spotipy

import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import json
from google.colab import drive

client_id = ‘ID’
client_secret = ’SECRET’
client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials(client_id, client_secret)
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

import csv
csvfile = open('/content/drive/MyDrive/regional-us-daily-latest.csv', encoding='utf-8')
csvreader = csv.DictReader(csvfile)

us = ("regional-us-daily-latest.csv", "us.csv")

for region in (us):
  inputfile = region[0]
  outputfile = region[1]
  songs = pd.read_csv(inputfile, index_col=0, header=1)
  songs = songs.assign(Genre=0)
  for index, row in songs.iterrows():
    artist = row["Artist"]
    result = spotify.search(artist, limit=1, type="artist")
    genre = result["artists"]["items"][0]["genres"]
    songs['Genre'][index] = genre
  songs.head(10)
  songs.to_csv(outputfile)
  files.download(outputfile)

CodePudding user response:

Save the csv file in the Google drive and go to your notebook click on drive and search for your file in the drive . Then copy the path of the csv file in a variable and use the variable using read_csv() method

CodePudding user response:

  1. please mount the drive first

    
    
         from google.colab import drive
         drive.mount('/content/drive')
    
     
  2. Change the directory to MyDrive and check current directory

    
    
         import os
         os.chdir("drive/My Drive/")
         print(os.getcwd())
         !ls
    
     
  3. Set the path of file. and use source_file variable where file name required

    source_file = os.path.join(os.getcwd(), "regional-us-daily-latest.csv")
  • Related