Creating a file in another folder in python

I am trying to create an image file using opencv in python. when i create it in the same folder file created

face_file_name = "te.jpg" cv2.imwrite(face_file_name, image) 

but when I try to create it in another folder, for example

  face_file_name = "test\te.jpg" cv2.imwrite(face_file_name, image) 

the file is not created. can someone explain the reasons?

I even tried to give an absolute path. I am using python2.7 in windows.

+4
source share
2 answers

cv2.imwrite() will not write the image to another directory if the directory does not exist. First you need to create a directory before trying to write to it:

 import os dirname = 'test' os.mkdir(dirname) 

Here you can either write to the directory without changing the working directory:

 cv2.imwrite(os.path.join(dirname, face_file_name), image) 

Or change the working directory and omit the directory prefix, depending on your needs:

 os.chdir(dirname) cv2.imwrite(face_file_name, image) 
+15
source

You can try this ... I am working on python 3.6 on Windows. First you need to create the directory in which you want to save, and then you can use this command.

 cv2.imwrite("full/path/ofThe/folder/te.jpg", image) 

or you can write it like this:

 cv2.imwrite("full/path/ofThe/folder/"+face_file_name, image) 
0
source

Source: https://habr.com/ru/post/1490141/


All Articles