遍历目录中的文件并使用文件名作为变量,并将文件路径分配给变量
iterate over files in directory and use file names as variables, and assign the file path to the variable
我试图遍历一个文件夹并使用 Pyspark 在 DataBricks 中获取文件名和这些文件的路径。
突然间,一个想法出现了,如果我们可以将文件名作为变量并将路径分配给相应的名为变量的文件。
我们可以使用 dbutils 创建小部件并将文件名指定为参数,以使事情变得更容易。
因此,在这个过程中,我一直在获取文件和文件名的路径。
但是我无法弄清楚变量的创建以及在各个文件名变量中分配各个文件的路径
这是代码:
import pandas as pd
import os
list1 =[]
list2 =[]
directory='/dbfs/FileStore/tables'
dir='/FileStore/tables'
for filename in os.listdir(directory):
if filename.endswith(".csv") or filename.endswith(".txt"):
file_path=os.path.join(dir, filename)
print(file_path)
print(filename)
list1.append(file_path)
list2.append(filename)
提前致谢
如果您设置为使用文件名为变量分配路径,那么您可以尝试:
...
for filename in os.listdir(directory):
if filename.endswith(".csv") or filename.endswith(".txt"):
file_path=os.path.join(dir, filename)
print(file_path)
print(filename)
exec("%s = '%s'" % (filename, file_path))
注意附加的引号集避免了语法和名称错误。然而,这个解决方案仍然充满了问题。例如,看起来对 exec
的调用将文件路径中的反斜杠作为 unicode:
filename = 'file1'
filepath = '\maindir\foo'
exec("%s = '%s'" % (filename, filepath))
file1
'\maindir\x0coo'
但字典似乎更适合他的情况:
...
filenames_and_paths = {}
for filename in os.listdir(directory):
if filename.endswith(".csv") or filename.endswith(".txt"):
file_path=os.path.join(dir, filename)
print(file_path)
print(filename)
filenames_and_paths[filename] = file_path
不确定为什么要为名称和路径创建两个列表,但如果需要它们,您也可以使用字典理解:
filenames_and_paths = {name:path for name,path in zip(list1, list2)}
对于 Pyspark,我宁愿建议使用 Hadoop FS API 来列出文件,因为 os.listdir
无法与外部 buckets/storage.
一起使用
这是您可以改编的示例:
# access hadoop fs via the JVM
Path = sc._gateway.jvm.org.apache.hadoop.fs.Path
conf = sc._jsc.hadoopConfiguration()
# list directory
directory = Path("/dbfs/FileStore/tables/*.csv")
gs = directory.getFileSystem(conf).globStatus(directory)
# create tuples (filename, filepath), you can also filter specific files here...
paths = []
if gs:
paths = [(f.getPath().getName(), f.getPath().toString()) for f in gs]
for filename, file_path in paths:
# your process
我试图遍历一个文件夹并使用 Pyspark 在 DataBricks 中获取文件名和这些文件的路径。 突然间,一个想法出现了,如果我们可以将文件名作为变量并将路径分配给相应的名为变量的文件。 我们可以使用 dbutils 创建小部件并将文件名指定为参数,以使事情变得更容易。 因此,在这个过程中,我一直在获取文件和文件名的路径。 但是我无法弄清楚变量的创建以及在各个文件名变量中分配各个文件的路径 这是代码:
import pandas as pd
import os
list1 =[]
list2 =[]
directory='/dbfs/FileStore/tables'
dir='/FileStore/tables'
for filename in os.listdir(directory):
if filename.endswith(".csv") or filename.endswith(".txt"):
file_path=os.path.join(dir, filename)
print(file_path)
print(filename)
list1.append(file_path)
list2.append(filename)
提前致谢
如果您设置为使用文件名为变量分配路径,那么您可以尝试:
...
for filename in os.listdir(directory):
if filename.endswith(".csv") or filename.endswith(".txt"):
file_path=os.path.join(dir, filename)
print(file_path)
print(filename)
exec("%s = '%s'" % (filename, file_path))
注意附加的引号集避免了语法和名称错误。然而,这个解决方案仍然充满了问题。例如,看起来对 exec
的调用将文件路径中的反斜杠作为 unicode:
filename = 'file1'
filepath = '\maindir\foo'
exec("%s = '%s'" % (filename, filepath))
file1
'\maindir\x0coo'
但字典似乎更适合他的情况:
...
filenames_and_paths = {}
for filename in os.listdir(directory):
if filename.endswith(".csv") or filename.endswith(".txt"):
file_path=os.path.join(dir, filename)
print(file_path)
print(filename)
filenames_and_paths[filename] = file_path
不确定为什么要为名称和路径创建两个列表,但如果需要它们,您也可以使用字典理解:
filenames_and_paths = {name:path for name,path in zip(list1, list2)}
对于 Pyspark,我宁愿建议使用 Hadoop FS API 来列出文件,因为 os.listdir
无法与外部 buckets/storage.
这是您可以改编的示例:
# access hadoop fs via the JVM
Path = sc._gateway.jvm.org.apache.hadoop.fs.Path
conf = sc._jsc.hadoopConfiguration()
# list directory
directory = Path("/dbfs/FileStore/tables/*.csv")
gs = directory.getFileSystem(conf).globStatus(directory)
# create tuples (filename, filepath), you can also filter specific files here...
paths = []
if gs:
paths = [(f.getPath().getName(), f.getPath().toString()) for f in gs]
for filename, file_path in paths:
# your process