0

I am working on a script where I need to created a sub-directory inside a directory. The main directory contains a large number of .tif images (8000-9000) of which I am sorting into the sub-directory based on specific conditions of the image information.

My issue is that when I use os.path.exists and os.path.isdir, both yield 'True'. However, when using terminal to access the sub-directory (or even through VSCode), it is said to not exist.

Example of what I have done in terms of code given here:

Edit: providing the absolute path of the directory and sub-directory I would like to create.

import os 
import shutil 
import cv2
import numpy as np

path = '/home/proy/pm/AF_patch/block_01_01_AF_reg'   #contains .tif files
path2 = 'background_removed_patch' 
new_path = os.path.join(path, path2)
print(new_path)  # yields correct sub-directory to be created

os.makedirs(new_path)
print(os.path.exists('/pm/AF_patch/block_01_01_AF_reg/background_removed_patch')) #yield true 
print(os.path.isdir('/pm/AF_patch/block_01_01_AF_reg/background_removed_patch')) #yield true 

Despite the path being said to exist, when trying to access the new directory through terminal, I get:

(base) [proy@login-0 block_01_01_AF_reg]$ cd background_removed_patch
bash: cd: background_removed_patch: No such file or directory

Any ideas or information regarding a solution for this would be much appreciated. Essentially I am trying to create this directory (absolute path): /home/proy/pm/AF_patch/block_01_01_AF_reg/background_removed_patch

Current working directory from os.getcwd is given as `/home/proy/pm'.

Thank you for the help.

prernaroy
  • 95
  • 1
  • 1
  • 6
  • You created `pm/AF_patch/block_01_01_AF_reg/background_removed_patch`, not `background_removed_patch` – tripleee Mar 02 '22 at 06:32
  • @tripleee I think I understand this to be the issue too. How would I make background_removed_patch a subdirectory inside block_01_01_AF_reg in that case? Thank you for your answer. – prernaroy Mar 02 '22 at 06:43
  • If you want to create `background_removed_patch` in the current working directory, `os.makedirs('background_removed_patch')`. Your question is unclear as to where you are in the file system and what you expect; if the duplicate doesn't solve your problem, please explain in more detail how exactly this is different. Adding absolute paths to your explanation would probably be useful. – tripleee Mar 02 '22 at 06:46
  • @tripleee Thank you for your reply. I have updated it with the absolute paths. `os.makedirs(background_removed_patch` unfortunately did not create the directory in the correct folder. I want it to be inside `block_01_01_AF_reg`. – prernaroy Mar 02 '22 at 06:58
  • None of these paths are absolute. An absolute path always starts with `/`. Perhaps see also [Difference between `./` and `~/`](https://stackoverflow.com/questions/31435921/difference-between-and/55342466) – tripleee Mar 02 '22 at 07:00
  • The prompt in your interactive example suggests that your current working directory is actually `pm/AF_patch/block_01_01_AF_reg` in which case the command should work, but this needs to be explicit, too; you could have another directory whose name is `block_01_01_AF_reg` and then the problem is that you are confused about your current working directory, as originally diagnosed. – tripleee Mar 02 '22 at 07:01
  • Understood your point. After changing the working directory explicitly to `pm/AF_patch/block_01_01_AF_reg`, the `os.makedirs` worked as intended. Thank you for the help! – prernaroy Mar 02 '22 at 07:17
  • For what it's worth, the slash in `/pm` makes the program look for a directory `pm` in the root of your drive; here, you want either the full absolute path `/home/proy/pm`, or a relative path `pm` – tripleee Mar 02 '22 at 07:33
  • Why don't you do a `cd ~proy/pm/AF_patch/block_01_01_AF_re/background_removed_patch`, since this is the directory you created.... – user1934428 Mar 02 '22 at 07:36
  • @tripleee Using the absolute path led to an error `PermissionError: [Errno 13] Permission denied`, whilst using the relative path would create the folder in the wrong place. @user1934428 your suggested command yielded the same issue that I had in the original question, unfortunately. Specifying the working directory to be `block_01_01_AF_reg` seemed to resolve the problem I was having. – prernaroy Mar 02 '22 at 08:42
  • Of course, you need to use the _correct_ absolute path, like I tried to explain in my previous comment. Probably review the questions I pointed to already and perhaps look for a beginner-friendly introduction to the file system if you need more help. – tripleee Mar 02 '22 at 11:26

0 Answers0