0

I have been having issues when trying to insert information to listdir.

initially, I have tried to manually put in the info:

for file in os.listdir(r'H:\My Documents')

where I get this error: [Errno 2] No such file or directory: 'H:\\My Documents'

then the below

list_of_directory = r'H:\My Documents' 
for file in os.listdir(list_of_directory):

to which I got the same error.

I have tried everything including the solution mentioned here Error with double backslash in Windows path in Python but it wouldn't work.

can you please help? The rest of the code is ready and it's so frustrating that it has to wait due to this glitch!

quamrana
  • 37,849
  • 12
  • 53
  • 71
gestak
  • 1
  • Are you very sure there *is* a folder called `H:\My Documents`? What happens if you do `dir "H:\My Documents"` at a console prompt? – BoarGules Apr 06 '22 at 12:02
  • Yes, there definitely is. I'm not sure I'm getting what you're saying about dir, as I'm new to Python. Should I replace any part of the code? – gestak Apr 06 '22 at 13:13
  • I was asking you to type `dir "H:\My Documents"` at a _Windows console prompt_. Nothing to do with Python. I know you believe definitely that the folder does exist. But in my experience, a Windows folder called `My Documents` is typically a subsubfolder of `Users` and so is *not* at the root directory level. So I was asking you to cut and paste a command into a Windows console session that you had not typed yourself that would confirm or disconfirm your conviction with a couple of keystrokes. Retyping it could reintroduce a previous error. I do this myself when I hit this sort of problem. – BoarGules Apr 06 '22 at 16:32
  • Thanks. It makes sense. Tbh I looked into it a bit further. The Documents folder was just an example. I was looking to incorporate a samba link into the code but I see there are multiple issues with this type of links due to their own nature. – gestak Apr 07 '22 at 12:39

2 Answers2

0

You should just be able to use a forwards slash instead on any OS, because the os library replaces forwards slahes with baclwards slashes for Windows. If that doesn't work, you could try using the pathlib library.

from pathlib import Path
data_folder = Path("source_data/text_files/")
file_to_open = data_folder / "raw_data.txt"

If you wanted, you could even use pathlib to convert a Unix to a Windows path.

from pathlib import Path, PureWindowsPath
filename = Path("source_data/text_files/raw_data.txt")
path_on_windows = PureWindowsPath(filename)

So your code would become:

from pathlib import Path
list_of_directory = Path('H:/My Documents').resolve()
for file in os.listdir(list_of_directory):
  print(file)
OctopuSS7
  • 465
  • 2
  • 9
0

Did you mean to catch the file not found error:

list_of_directory = r'H:\My Documents'
try:
    for file in os.listdir(list_of_directory):
        ...
except FileNotFoundError:
    print(list_of_directory, "not found")

This will allow the program to continue and do something else when the folder does not exist.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • I need the folder to exist, as I will need to access it to retrieve some files – gestak Apr 06 '22 at 13:13
  • Well, this code will tell you that the folder doesn't exist, so you will know that there are no files to retrieve. – quamrana Apr 06 '22 at 14:54