Issue
I'm using pandas to read a csv file, the code runs pretty well when I run it on Spyder, but then, when I try running it on anaconda prompt or even double clicking on the .py file it does not run. On anaconda prompt I get "FileNotFoundError: [Errno 2] File b Usuarios.csv' does not exist: b Usuários.csv'" error, even if the file does exists and it is there in the same directory as the .py file. I am using this code:
import pandas
import csv
csv_reader = pandas.read_csv("D:\\clovi\\Projetos\\Python\\Usuarios.csv",encoding='utf-8')
j=0
y = csv_reader.iloc[-1].values[0]
while True:
i=0
x = csv_reader.iloc[j].values
usuario = x[i]
i+=1
pase = x[i]
j+=1
if usuario == y:
edistribucion(usuario,pase)
break
else:
edistribucion(usuario,pase)
NOTE: For reading the file, I also tried doing
import pandas as pd
import csv
csv_reader = pd.read_csv("Usuarios.csv")
.
.
.
As I said, it runs well when I'm running on Spyder, but it doesn't run anywhere else. What am I doing wrong on the code?
Solution
It might be that the Anaconda prompt does not have permission to access the directory where the CSV file is located. In that case you could modify your script to accept a command-line argument, and if there is none, default to the path you already have, something like this:
import sys
import os
import pandas
import csv
# If there is a command-line argument, and the argument is a valid file, this matches
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
csv_path = sys.argv[1]
else:
csv_path = r'D:\clovi\Projetos\Python\Usuarios.csv'
csv_reader = pandas.read_csv(csv_path,encoding='utf-8')
j=0
y = csv_reader.iloc[-1].values[0]
while True:
i=0
x = csv_reader.iloc[j].values
usuario = x[i]
i+=1
pase = x[i]
j+=1
if usuario == y:
edistribucion(usuario,pase)
break
else:
edistribucion(usuario,pase)
You'd be able to dynamically change the CSV without breaking the code, so in the command-line:
# This tries to process the hard-coded CSV
python clovis_magno.py
# While this processes top_secret_data.csv in your Desktop
python clovis_magno.py "C:\Users\Clovis Magno\Desktop\top_secret_data.csv"
Answered By - João Amaro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.