在 python 中导入多个文件
Importing multiple files in python
我正在尝试创建一个脚本,用户可以在其中 select 文件夹中的 1 个或所有文件("imitate" [=27] 的多 select 离子打开=]uigetfile 在 Matlab 中)。之后,脚本会询问用户是否要从另一个位置导入数据,导入 1 或所有例程继续。
脚本的任务只是检索多select离子选项的路径和文件名。它是在 Anaconda Distro 中使用 Windows 10、Python 3.6 和 Spyder 作为 IDE 在 PC 上编写的。
到目前为止我有这个:
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()
script/function returns 完整路径和文件名,但是,某些名称由于某种原因带有双反斜杠
例如
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.
任何人都可以看看这个脚本,看看是否有问题,或者这是否只是 Python 显示内容的方式。
提前致谢!
\
就是 Python 显示转义反斜杠的方式。 \
在许多上下文中用于表示换行 (\n
)、制表符 (\t
) 等。因此 \
仅表示反斜杠后出现的任何内容不是这些特殊字符之一。
Python 会理解你的正斜杠和反斜杠的混合,但如果你想让所有内容显示一致,你可以使用 [os.path.abspath(d) for d in my_list]
.
此外,如果您想避免创建列表列表,您似乎应该使用 extend
而不是 append
。
这里是"final"版本
谢谢大家!
此函数检索文件夹中 1 个或所有文件的文件位置。
def import_multiple_files():
# 'similar' to UIGETFILE
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import glob
import os
# Creates a Tkinter window to search for a file
root = tk.Tk()
root.withdraw()
root.attributes("-topmost", True)
root.lift()
file_location = filedialog.askopenfilename()
a=file_location.split('/')
# Separates the file location into path and file name
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")
allFiles.extend(b)
else:
b=[(path + "/"+ filename)]
allFiles.extend(b)
# Questions the user
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")
# Allows the user to import as many files from as as many folders as he/she chooses
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.extend(b)
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")
else:
b=[(path + "/"+ filename)]
allFiles.extend(b)
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")
else:
finish=1
b=[os.path.abspath(d) for d in allFiles]
# Returns all file locations
return(b)
我正在尝试创建一个脚本,用户可以在其中 select 文件夹中的 1 个或所有文件("imitate" [=27] 的多 select 离子打开=]uigetfile 在 Matlab 中)。之后,脚本会询问用户是否要从另一个位置导入数据,导入 1 或所有例程继续。
脚本的任务只是检索多select离子选项的路径和文件名。它是在 Anaconda Distro 中使用 Windows 10、Python 3.6 和 Spyder 作为 IDE 在 PC 上编写的。
到目前为止我有这个:
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()
script/function returns 完整路径和文件名,但是,某些名称由于某种原因带有双反斜杠
例如
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.
任何人都可以看看这个脚本,看看是否有问题,或者这是否只是 Python 显示内容的方式。
提前致谢!
\
就是 Python 显示转义反斜杠的方式。 \
在许多上下文中用于表示换行 (\n
)、制表符 (\t
) 等。因此 \
仅表示反斜杠后出现的任何内容不是这些特殊字符之一。
Python 会理解你的正斜杠和反斜杠的混合,但如果你想让所有内容显示一致,你可以使用 [os.path.abspath(d) for d in my_list]
.
此外,如果您想避免创建列表列表,您似乎应该使用 extend
而不是 append
。
这里是"final"版本
谢谢大家!
此函数检索文件夹中 1 个或所有文件的文件位置。
def import_multiple_files():
# 'similar' to UIGETFILE
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import glob
import os
# Creates a Tkinter window to search for a file
root = tk.Tk()
root.withdraw()
root.attributes("-topmost", True)
root.lift()
file_location = filedialog.askopenfilename()
a=file_location.split('/')
# Separates the file location into path and file name
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")
allFiles.extend(b)
else:
b=[(path + "/"+ filename)]
allFiles.extend(b)
# Questions the user
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")
# Allows the user to import as many files from as as many folders as he/she chooses
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.extend(b)
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")
else:
b=[(path + "/"+ filename)]
allFiles.extend(b)
qst=messagebox.askyesno("Multiple Import","Do you want to import more DATA?")
else:
finish=1
b=[os.path.abspath(d) for d in allFiles]
# Returns all file locations
return(b)