Remove folder in blob in a more effective way - python

Suppose I am going to remove a folder from the blob from databricks. however when the folder is not empty , it throws the error "non-empty" directory is not permitted using dbutils.fs.rm("wabs://..../folder")
Even has error include recurse=True
How to solve it?

Note: Delete the file or directory (optionally recursively delete all files in the directory). This call throws an exception with IO_ERROR if the path is a non-empty directory and recursive is set to false or on other similar errors.
For more details, refer similar SO thread.
Hope this helps.

According to here, the below function helps me solve this problem.
def delete_mounted_dir(dirname):
files=dbutils.fs.ls(dirname)
for f in files:
if f.isDir():
delete_mounted_dir(f.path)
dbutils.fs.rm(f.path, recurse=True)

Related

Exception on extracting a zipfile, after appending to it in Python

z = zipfile.ZipFile(io.BytesIO(artifact), mode='a')
z.write("test.txt",arcname=r'bin/test.txt')
z.extractall('out')
Exception:
zipfile.BadZipFile was unhandled by user code
Message: File name in directory 'bin\test.txt' and header b'bin/test.txt' differ.
The interesting thing is if I write the file to disk, and try extract it, I get a invalid file error. This is on Win 7 by the way.
the bin folder already exists in the zipfile. Full Traceback
Actually the code works well on my Mac,and I think you should let us know that the structure of the zip file or what the variable artifact is.
Here is my advice:
Use forward slashes as path separators,when you create the zip file.
Try to print a warning not raise the exception,and check out the out folder,you will find the reason,maybe the slashes or string buffer.
Also you can read this issue.
Hope this helps.

Errno13, Permission denied when trying to read file

I have created a small python script. With that I am trying to read a txt file but my access is denied resolving to an no.13 error, here is my code:
import time
import os
destPath = 'C:\Users\PC\Desktop\New folder(13)'
for root, dirs, files in os.walk(destPath):
f=open(destPath, 'r')
.....
Based on the name, I'm guessing that destPath is a directory, not a file. You can do a os.walk or a os.listdir on the directory, but you can't open it for reading. You can only call open on a file.
Maybe you meant to call open on one or more of the items from files
1:
I take it you are trying to access a file to get what's inside but don't want to use a direct path and instead want a variable to denote the path. This is why you did the destPath I'm assuming.
From what I've experienced the issue is that you are skipping a simple step. What you have to do is INPUT the location then use os.CHDIR to go to that location. and finally you can use your 'open()'.
From there you can either use open('[direct path]','r') or destPath2 = 'something' then open(destPath2, 'r').
To summarize: You want to get the path then NAVIGATE to the path, then get the 'filename' (can be done sooner or not at all if using a direct path for this), then open the file.
2: You can also try adding an "r" in front of your path. r'[path]' for the raw line in case python is using the "\" for something else.
3: Try deleting the "c:/" and switching the / to \ or vice versa.
That's all I got, hope one of them helps! :-)
I got this issue when trying to create a file in the path -C:/Users/anshu/Documents/Python_files/Test_files . I discovered python couldn't really access the directory that was under the user's name.
So, I tried creating the file under the directory - C:/Users/anshu/Desktop .
I was able to create files in this directory through python without any issue.

Error, file already exists. Python

When I am using shutil, I get an unexpected error:
System error 183. Cannot create file when that file already exists
I am using this:
shutil.copytree(src,dst)
src,dst are paths to my directories which I would like to copy. Names are different. For example:
src = 'D:\test\tmp\dir1'
dst = 'D:\test\tmp\dir2'
I know, I could delete dir2 and everything is ok, but I would like to do it without this, is it possible with shutil ?
The document for shutil specifically says that the destination directory must not exist. This happens because it makes a os.makedirs(dst). If you want to append files it could be useful if you used shutil.copyfile.
I am not sure if using shuthil is possible here. Perhaps you can save as a new file?

Delete multiple directories in python

In python, I understand that I can delete multiple files with the same name using the following command for eg:
for f in glob.glob("file_name_*.txt"):
os.remove(f)
And that a single directory can be deleted with shutil.rmtree('/path/to/dir') - and that this command will delete the directory even if the directory is not empty. On the other hand, os.rmdir() needs that the directory be empty.
I actually want to delete multiple directories with the same name, and they are not empty. So, I am looking for something like
shutil.rmtree('directory_*')
Is there a way to do this with python?
You have all of the pieces: glob() iterates, and rmtree() deletes:
for path in glob.glob("directory_*"):
shutil.rmtree(path)
This will throw OSError if one of the globbed paths names a file, or for any other reason that rmtree() can fail. You can add error handling as you see fit, once you decide how you want to handle the errors. It doesn't make sense to add error handling unless you know what you want to do with the error, so I have left error handling out.

python zipfile basename

I have some homework that I am trying to complete. I don't want the answer. I'm just having trouble in starting. The work I have tried is not working at all... Can someone please just provide a push in the right direction. I am trying to learn but after trying and trying I need some help.
I know I can you os.path.basename() to get the basename and then add it to the file name but I can't get it together.
Here is the assignment
In this project, write a function that takes a directory path and creates an archive of the directory only. For example, if the same path were used as in the example ("c:\\xxxx\\Archives\\archive_me"), the zipfile would contain archive_me\\groucho, archive_me\\harpo and archive_me\\chico.
The base directory (archive_me in the example above) is the final element of the input, and all paths recorded in the zipfile should start with the base directory.
If the directory contains sub-directories, the sub-directory names and any files in the sub-directories should not be included. (Hint: You can use isfile() to determine if a filename represents a regular file and not a directory.)
Thanks again any direction would be great.
It would help to know what you tried yourself, so I'm only giving a few pointers to methods in the standard libraries:
os.listdir to get the a list of files and folders under a given directory (beware, it returns only the file/folder name, not the full path!)
os.path.isfile as mentioned in the assignment to check if a given path represents a file or a folder
os.path.isdir, the opposite of os.path.isfile (thanks inspectorG4adget)
os.path.join to join a filename with the basedir without having to worry about slashes and delimiters
ZipFile for handling, well, zip files
zipFile.write to write the files found to the zip
I'm not sure you'll need all of those, but it doesn't hurt knowing they exist.

Categories