1

I want to copy a file to a new non-existing folder:

import pandas as pd
from imutils import paths
from os import path, makedirs
from shutil import copyfile
from pathlib import Path
import os

imagePaths = list(paths.list_images('/x/x/x/x/DatasetNiiCleanedcopyB/test/NCP/'))
df= pd.read_csv(r"file.csv")

    # loop over the image paths
    for imagePath in imagePaths:
        word='/'.join(imagePath.split('/')[7:])
        #search in dataframe
        if((df['imgpath'].str.contains(word)).any()):
            imPath = Path(imagePath)
            destination_path= imPath.parent.absolute()
            output = str(destination_path).replace('DatasetNiiCleanedcopyB', 'DatasetNiiCleanedcopyB3')+'/' 
            print('source path is'+ imagePath)
            print('destination path is'+ output)   
            makedirs(path.dirname(path.abspath(output)), exist_ok=True)
            copyfile(imagePath, output)
        

Output:

source path is=  /x/x/x/x/DatasetNiiCleanedcopyB/test/NCP/61/1255/0065.png
    
destination path is= /x/x/x/x/DatasetNiiCleanedcopyB3/test/NCP/61/1255/
         
   

The code works well, but copyfile raises this error:

FileNotFoundError: [Errno 2] No such file or directory: /x/x/x/x/DatasetNiiCleanedcopyB3/test/NCP/61/1255/

   

I don't know why not the file is not copied?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Rawan
  • 373
  • 1
  • 3
  • 17
  • You need to provide the filename in the destination too, when copying. About the question, it has formatting problems and the code section contains syntax errors given indentation is critical in Python. – MatBBastos Sep 16 '21 at 15:45
  • The destination path you printed is not what you passed to `makedirs`: i.e. the `1255` subfolder was not created. – ekhumoro Sep 16 '21 at 15:47
  • Does this answer your question? [How do I copy a file in Python?](https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) – MatBBastos Sep 16 '21 at 15:48
  • @MatBBastos No, because the error relates to a directory, not a file. – ekhumoro Sep 16 '21 at 15:49
  • To be clear, you should simply do `makedirs(output, exist_ok=True)`. (Note that `output` is already absolute since it's derived from `destination_path`). – ekhumoro Sep 16 '21 at 16:00
  • Thank you can you @ekhumoro write it as answer to accept it? – Rawan Sep 16 '21 at 16:05
  • @Rafa Glad you got it working. I have added an answer. – ekhumoro Sep 16 '21 at 16:08
  • Also, you don't need `path` and `makedirs` from `os` if you're using `pathlib.Path`. The latter has everything you need – MatBBastos Sep 16 '21 at 17:02

1 Answers1

0

The destination path you printed is not what you actually passed to makedirs. The terminal subfolder was not created because you passed the parent of output. But that was not necessary, because output is derived from destination_path, which is already the (absolute) parent of the relevant folder. So all you really need is this:

makedirs(output, exist_ok=True)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336