5

I'm a noob at coding Python and I've run into something that no amount of Googling is helping me with. I'm trying to write a simple Directory listing tool and I cannot seem to deal with Spaces in the directory name in OSX. My code is as follows:

def listdir_nohidden(path):
    import os
    for f in os.listdir(path):
        if not f.startswith('.'):
            yield f

def MACListDirNoExt():
import os
MACu = PCu = os.environ['USER']
MACDIR = '/Users/'+MACu+'/Desktop//'
while True:
    PATH = raw_input("What is the PATH you would like to list?")
    if os.path.exists(PATH):
        break
    else:
        print "That PATH cannot be found or does not exist."
NAME = raw_input ("What would you like to name your file?")
DIR = listdir_nohidden(PATH)
DIR = [os.path.splitext(x)[0] for x in DIR]
f = open(''+MACDIR+NAME+'.txt', "w")
for file in DIR:
    f.write(str(file) + "\n")
f.close()
print "The file %s.txt has been written to your Desktop" % (NAME)
raw_input ("Press Enter to exit")

For ease of trouble shooting though I think this could essentially be boiled down to:

import os
PATH = raw_input("What is the PATH you would like to list")
os.listdir(PATH)

When supplying a directory path that contains spaces /Volumes/Disk/this is a folder it returns

"No such file or Directory: '/Volumes/Disk/this\\ is\\ a\\ folder/'

It looks like its escaping the escape...?

dennis
  • 83
  • 1
  • 2
  • 6
  • The code shown cannot produce the error message shown. Please show the code you are actually running, *unedited*, the exact input you gave it, *unedited*, and the error message you actually got, *unedited*. – zwol Apr 11 '16 at 18:05
  • Note also that your second code fragment (the "boiled down" one) works correctly on directories with spaces in their names, and your first code fragment has become misindented and therefore I can't test it (don't use tabs). – zwol Apr 11 '16 at 18:07
  • (Is your problem maybe that you are typing backslashes at your prompt? Don't do that. The shell wants that, but this program doesn't.) – zwol Apr 11 '16 at 18:09
  • The last snippet works fine if the input is with spaces but no backslashes. – trans1st0r Apr 11 '16 at 18:19
  • OK, here's an update. The code works fine when typing in your Directory. The problem occurs when I drag the directory I want to list into the the terminal window the application is running in. the shell automatically writes the path as /Volumes/Disk/This\ is\ a\ folder\ It is entering its own escapes and passing that to the raw_input. Is there a way to account for that? – dennis Apr 11 '16 at 18:26
  • zwol, You are correct neglected to realize that when dragging the folder into the applications shell window it added backslashes. is there a way to account for that? I can guarantee Users will drag and drop. – dennis Apr 11 '16 at 18:33
  • @dennis you can try checking the value returned from raw_input() for occurences of '\\' and replace them with ''. `a = a.replace('\\', '')` – trans1st0r Apr 11 '16 at 19:08
  • That did it! Thanks Everyone – dennis Apr 11 '16 at 19:18

2 Answers2

2

Check the value returned from raw_input() for occurences of '\\' and replace them with ''.

a = a.replace('\\', '')
trans1st0r
  • 2,023
  • 2
  • 17
  • 23
2

I just ran into this, and I'm guessing that what I was hastily doing is also what you were trying. In a way, both @zwol and @trans1st0r are right.

Your boiled down program has nothing wrong with it. I believe that if you put in the input /Volumes/Disk/this is a folder, everything would work fine.

However, what you may have been doing (or at least, what I was doing) is dragging a folder from the Finder to the Terminal. When you drag to the Terminal, the OS automatically escapes spaces for you, so what ends up getting typed into the Terminal is /Volumes/Disk/this\ is\ a\ folder.

So either you can make sure that what you "type in" doesn't have those backslashes, or you can use @trans1st0r's suggestion as a way to support the dragging functionality, though the latter will cause issues in the edge case that your desired path actually has backslashes in it.

leekaiinthesky
  • 5,413
  • 4
  • 28
  • 39