I'm trying to loop through files in a directory, but I keep getting a file not found error, despite the fact the directory is literally copy-pasted from Windows explorer. It's not stored in the C drive, so might that be the problem?
Here's the code.
# Setting directory
obj_path = "D:/ultivue/1604-2201 Core 8plex (All Runs) Region Object Data/run 2"
# For loop to go through all files in the path
for file in obj_path:
data = pd.read_csv(file)
And here's the error.
FileNotFoundError: [Errno 2] No such file or directory: 'D'
Strings are iterables in Python, which means they can be looped over. So something like
for char in 'word': print(char)will print each character of'word'individually.As written, you're trying to iterate over the string
obj_pathwithfor _ in, which is why it's stopping at 'D' (the first letter of the string) and throwing an error when it tries to open that as a csv.You can use the
pathlibmodule'siterdir()method to iterate over all the files in a given directory