'unsupported operand type(s) for /: 'str' and 'str' when reading .CSV file in directory
I have a program that reads the first .CSV file in a given directory. However, at the end of my program it gives an error on line 9 reading unsupported operand type(s) for /: 'str' and 'str'.
I'm unsure why this happens when firstfile is called exactly 18949a_2020110-02_QUVA_1400hr_ABS_BC_AS0001.CSV
The program is given below:
import os
from pathlib import Path
import pandas as pd
fileDir = Path("C:/Users/Jonas/Desktop/Test")
folder_walk = os.walk(fileDir)
first_file_in_folder = next(folder_walk)[2][0]
firstfile = first_file_in_folder.replace(" ","_") #Just adds hyphens to the string name
data=pd.read_csv("C:/Users/Jonas/Desktop/Test"/firstfile) #Reads the .CSV file but gives the error
Thanks
Solution 1:[1]
You have this line of code here: data=pd.read_csv("C:/Users/Jonas/Desktop/Test/"/firstfile).
Your issue is with the /firstfile, because python ends up trying to divide "C:/Users/Jonas/Desktop/Test" by the string variable, 'firstfile'.
Change the last line to data=pd.read_csv("C:/Users/Jonas/Desktop/Test/"+firstfile), so it adds the strings together instead of dividing them.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ouflak |
