I'm trying to load a lot of files from a directory. I used to be able to do it by having this
#directory where all data will be stored
dataDir="C:/Users/me/Desktop/Data/"
Files=[] #list of files
for file in os.listdir(dataDir):
Files.append(scipy.io.loadmat(dataDir+file))
But now, I'm trying to have the user select the folder so I have this:
import tkinter
from tkinter import filedialog
from tkinter import *
root=tkinter.Tk()
filename=filedialog.askdirectory(parent=root,title='Choose a file')
print (filename)
#directory where all data will be stored
dataDir=('%s',filename)
Files=[] #list of files
for file in os.listdir(dataDir):
Files.append(scipy.io.loadmat(dataDir+file))
and it is giving me this error: "for file in os.listdir(dataDir): TypeError: listdir: path should be string, bytes, os.PathLike or None, not tuple)
I tried making filename into a string by doing str(filename), and it still wouldn't work. Any ideas?
When you define dataDir = ('%s', filename)
you are creating a tuple with two elements. One is '%s'
and the other the value of filename
.
If I understand correctly you shoud use dataDir = '%s' % filename
. That way dataDir
will be a string with the value of filename
.
You create tuple in command
dataDir=('%s',filename)
and you use it in listdir(dataDir)
which expect string
Use filename
directly in listdir
for file in os.listdir(filename):
The error states that the path you give listdir
should be a str
and that you gave it a tuple
.
With dataDir=('%s',filename)
, dataDir
is a tuple containing two strings. However, filename is already a str
. Instead of os.listdir(dataDir)
, try os.listdir(filename)
.
You will need to import os
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.