'Remove Files from Directory after uploading in Databricks using dbutils
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)
However, I'm getting the error:
TypeError: '/mnt/adls2/demo/target/' has the wrong type - class bool is expected.
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
Solution 1:[1]
If you want to delete all files from the following path: '/mnt/adls2/demo/target/', there is a simple command:
dbutils.fs.rm('/mnt/adls2/demo/target/', True)
Anyway, if you want to use your code, take a look at dbutils doc:
rm(dir: String, recurse: boolean = false): boolean -> Removes a file or directory
The second argument of the function is expected to be boolean, but your code has string with path:
dbutils.fs.rm(files[i].path, '/mnt/adls2/demo/target/' + file)
So your new code can be following:
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)
Solution 2:[2]
In order to remove the files from dbfs you can write this in any notebook
%fs rm -r dbfs:/user/sample_data.parquet
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jaroslav Bezděk |
| Solution 2 | Shikha |
