Manuel Oliveira Manuel Oliveira -3 years ago 205
Python Question

Importing multiple files in python

I'm trying to create a script where the user can select 1 or all files in a folder (to "imitate" the multi-selection open of uigetfile in Matlab). Afterwards, the script will ask if the user wants to import data from another location and the import 1 or all routine continues.

The mission of the script is just to retrieve the path and file names for a multi-selection option. It was written on a PC using Windows 10, with Python 3.6 and Spyder as a IDE in the Anaconda Distro.

So far I have this:

def import_multiple_files():
# Similar to UIGETFILE
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import glob


root = tk.Tk()
root.withdraw()
root.attributes("-topmost", True)
root.lift()
file_location = filedialog.askopenfilename()
a=file_location.split('/')
path=[]
for i in range(0,len(a)-1):
path.append(a[i])

path= "/".join(path)
filename=a[len(a)-1]

# Questions the user

qst=messagebox.askyesno("Multiple Import","Do you want to import all .txt files in this folder?")
allFiles=[]

if qst==True:

# Gets all .txt files in path FOLDER

b=glob.glob(path + "/*.txt") # glob. lists the filename and path

allFiles.append(b)
else:
b=(path + "/"+ filename)
allFiles.append(b)

qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")


finish=0
while finish==0:
if qst==True:

# deletes all variables except "AllFILES" (location of all files to import)
del(root,file_location,a,path,qst,b)

root = tk.Tk()
root.withdraw()
root.attributes("-topmost", True)
root.lift()
file_location = filedialog.askopenfilename()
a=file_location.split('/')
path=[]
for i in range(0,len(a)-1):
path.append(a[i])

path= "/".join(path)
filename=a[len(a)-1]

qst=messagebox.askyesno("Multiple Import","Do you want to import all .txt files in this folder?")

if qst==True:
# Gets all .txt files in path FOLDER
b=glob.glob(path + "/*.txt")
allFiles.append(b)
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")
else:
b=(path + "/"+ filename)
allFiles.append(b)
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")

else:
finish=1

return(allFiles)


file_location=import_multiple_files()


The script/function returns the full path and file name, however, some of the names came with a double backslash for some reason

e.g,

file_location
[['C:/Users/user/Desktop/New Folder (2)\\1.txt',
'C:/Users/user/Desktop/New Folder (2)\\2.txt',
'C:/Users/user/Desktop/New Folder (2)\\3.txt'],
['C:/Users/user/Desktop/New Folder (3)/1.txt']] # For this last file, I did not select the option of importing all files.


Can anyone be so kind as to take a look to this script and see if something is wrong, or if this is just the way Python displays things.

Thank you in advance!

Answer Source

The \\ is just the way Python shows an escaped backslash. \ is used in a number of contexts for signifying a new line (\n), a tab (\t) etc. So \\ just indicates that whatever appears after the backslash is not one of these special characters. Python will make sense of your mixture of forward and backslashes but if you want to have everything display consistently, you can use [os.path.abspath(d) for d in my_list].

Also, it looks like you should be using extend instead of append if you want to avoid creating a list of lists.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download