1

I am trying to pull folder names from a network drive in Python. For example, if I have a mapped drive U:, I want to go through and grab all the folders in folder "example" from the path \emily\hello\example.

This is what I have tried so far.

import os
os.chdir('U:')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
    dir = os.path.join('\emily\hello\example', dirs)
    os.chdir(dir)
    current = os.getcwd()
    new = str(current).split("\")[4]
    print(new)

this causes many errors, and I am having an issue with the syntax of a shared drive on the network vs a folder locally on my computer.

I want to see them in a list so i can read line by line and compare this list to another list.

ps. i don't want the files in the folders, just the folder names in the folder

thanks~!

error message is C:\Users\212582086\AppData\Local\Continuum\Anaconda3\python.exe "C:/Users/212582086/Desktop/Vendor sort/main" Traceback (most recent call last): File "C:/Users/212582086/Desktop/Vendor sort/main", line 6, in os.chdir(dir) FileNotFoundError: [WinError 3] The system cannot find the path specified: '\emily\hello\example\Software Archive'

Process finished with exit code 1

Emily
  • 100
  • 11
  • Can you include the error message and traceback? It's not clear from your question what goes wrong here. – Håken Lid Aug 31 '16 at 11:49
  • C:\Users\212582086\AppData\Local\Continuum\Anaconda3\python.exe "C:/Users/212582086/Desktop/Vendor sort/main" Traceback (most recent call last): File "C:/Users/212582086/Desktop/Vendor sort/main", line 6, in os.chdir(dir) FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\emily\\hello\\example\\Software Archive' Process finished with exit code 1 – Emily Aug 31 '16 at 11:50
  • What is the expected outcome here? There seems to be a directory on drive U called `U:\Software Archive\ `. But there's no directory with the name `\emily\hello\example\Software Archive\ `. `os.chdir()` fails when you pass it the name of a directory that doesn't exist. – Håken Lid Aug 31 '16 at 11:56
  • how do you format it though? regardless of exisiting or not... i don't understand. if i am in a U drive and it has a link or somthing with folders, how would i put it? – Emily Aug 31 '16 at 12:05
  • I don't understand what you mean by "how do you format it". You can't open a directory that doesn't exist. If you tell us what you expect the code to do, we can suggest how to fix it. – Håken Lid Aug 31 '16 at 12:11
  • i understand. my code now works. all that i don't understand how to do is find the path there. when i say format, i mean do i write it like 'c:/desktop/emily/example' or the backslashes? or if it's not on the computer, and on a network drive, do i have to inlude the link there? for example: if i hvae a file that looks like data(\\example03.fsr.read.com)(U:) and there are folders in there named \emily\example and i want to read all of the folders in example – Emily Aug 31 '16 at 12:20
  • Python is quite liberal with path names in windows. You can use forward slashes instead of backslash. http://stackoverflow.com/questions/2953834/windows-path-in-python I don't understand the rest of your question. – Håken Lid Aug 31 '16 at 12:25
  • i figured it out. i apologize! i stink at explaining things :) what i was wondering if i had to include the .com link part before the drive letter, but i didn't have to. just the drive letter with the location. – Emily Aug 31 '16 at 12:31

0 Answers0