Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python error:IsADirectoryError: [Errno 21] Is a directory

I am trying to decompress a list of .xz file and save them in a single folder,my code is show below

import shutil
path = "/content/drive/MyDrive/dataset_demo/Posts/"
output_path = "/content/sample_data/output/"
os.chdir(path)

for file_com in os.listdir(path):
  if file_com.endswith('.xz'):
    with lzma.open(file_com,'rb') as input:
      with open(output_path,'wb') as output:
        shutil.copyfileobj(input, output)

it noticed me that:"IsADirectoryError: [Errno 21] Is a directory: ‘/content/sample_data/output/’"
and I know the final aim should be a file but how could I save them in a folder

What should I do next, and thanks for your help and time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It looks like you’re trying to open the output_path as a file, but it’s actually a directory. You’ll need to specify the name of the file you want to write to within the output_path directory.

import shutil
import os
import lzma

path = "/content/drive/MyDrive/dataset_demo/Posts/"
output_path = "/content/sample_data/output/"
os.chdir(path)

for file_com in os.listdir(path):
  if file_com.endswith('.xz'):
    with lzma.open(file_com,'rb') as input:
      # Create the output file path
      output_file_path = os.path.join(output_path, file_com[:-3])
      with open(output_file_path,'wb') as output:
        shutil.copyfileobj(input, output)

In this code, I’ve added the os.path.join() function to create the output file path. This function combines the output_path directory with the file name (excluding the .xz extension) to create the full path to the output file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading