使用dbutils在Databricks中上传后,从目录中删除文件 [英] Remove Files from Directory after uploading in Databricks using dbutils
问题描述
StackOverflow的一位非常聪明的人协助我从Databricks将文件复制到目录中:
A very clever person from StackOverflow assisted me in copying files to a directory from Databricks here: copyfiles
复制文件后,我使用相同的原理删除文件,如链接:
I am using the same principle to remove the files once it has been copied as shown in the link:
for i in range (0, len(files)):
file = files[i].name
if now in file:
dbutils.fs.rm(files[i].path,'/mnt/adls2/demo/target/' + file)
print ('copied ' + file)
else:
print ('not copied ' + file)
但是,我遇到了错误:
TypeError:'/ mnt / adls2 / demo / target /'的类型错误-预期会出现类bool。
TypeError: '/mnt/adls2/demo/target/' has the wrong type - class bool is expected.
有人可以让我知道如何解决此问题。我认为在使用命令 dbutils.fs.rm
Can someone let me know how to fix this. I thought it would be simple matter of removing the file after originally copying it using command dbutils.fs.rm
推荐答案
如果要从路径'/ mnt / adls2 / demo / target /'删除所有文件
if you want to delete all files from the path '/mnt/adls2/demo/target/'
有一个简单的方法命令
dbutils.fs.rm('/ mnt / adls2 / demo / target /',True)
无论如何,如果您要使用代码,请查看dbutils 文档!
Anyway if you wanto to use your code take a look in dbutils doc!
rm(dir:字符串,递归:boolean = false) :boolean->删除文件或目录
您的代码具有更多参数
dbutils.fs.rm(files [i] .path,'/ mnt / adls2 / demo / target /'+ file)
我认为您不需要这部分,'/ mnt / adls2 / demo / target /'
I think you don't need this part ,'/mnt/adls2/demo/target/'
因此您的新代码可以为
for i in range (0, len(files)):
file = files[i].name
if now in file:
dbutils.fs.rm(files[i].path + file, True)
print ('copied ' + file)
else:
print ('not copied ' + file)
这篇关于使用dbutils在Databricks中上传后,从目录中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!