0

I've looked at a few posts regarding this topic but haven't seen anything specifically fitting my usage. This one looks to be close to the same issue. They talk about escaping the escape characters.

My issue is that I want this script to run on both MAC and PC systems - and to be able to process files in a sub-folder that has spaces in the folder name.

Right now I use this code gleaned partially from several different SO posts:

directory = raw_input("Enter the location of the files: ")
path = r"%s" % directory

for file in os.listdir(path):

I don't quite understand what the second line is doing - so perhaps that's the obvious line that needs tweaking. It works fine with regular folder names but not with spaces in the name.

I have tried using "\ " instead of just " ", - that didn't work - but in any case I'm looking for a code solution. I don't want the user to have to specify an escape character

On Windoze using sub folder name "LAS Data" in response to raw_input prompt (without quotation marks), I get an message that:

the system can't find the path specified "LAS Data\*.*"

Community
  • 1
  • 1
dbmitch
  • 5,361
  • 4
  • 24
  • 38
  • does **os.path.normpath** wouldn't work in this case. Asking because just didn't have nothing to do with MAC ever. – Take_Care_ Sep 06 '16 at 23:25
  • and as I see "LAS Data\*.*" is globing pattern ? – Take_Care_ Sep 06 '16 at 23:27
  • @Take_Care_ What are you suggesting? I can't understand your comment. Updated my question to show I am typing in "LAS Data" (without quotation marks) in response to raw_input parameter. – dbmitch Sep 06 '16 at 23:27
  • I'm sugesting using **os.path.normpath** function to produce valid path for any system You are using Your script on. – Take_Care_ Sep 06 '16 at 23:29
  • Where did you get that `path = r"%s" % directory` line? Link? – Stefan Pochmann Sep 06 '16 at 23:33
  • It was in some kind of search for using Python to process all files in a folder. I'll see if I can find it again. It seems to add the need wildcard to the folder name – dbmitch Sep 06 '16 at 23:37

2 Answers2

2

Your problem is most likely not with the code you present, but with the input you gave. The message

the system can't find the path specified "LAS Data\*.*"

suggest that you (or the user) entered the directory together with a wildcard for files. A directory with the name "LAS Data\*.*" indeed does not exist (unless you did something special to your file system :-).

Try entering just "LAS Data" instead.

The raw string should not be needed either

> python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> directory = raw_input("Enter the location of the files: ")
Enter the location of the files: Directory with Spaces
>>> for file in os.listdir(directory):
...     print(file)
...
hello-1.txt
hello-2.txt
>>>
M. Wymann
  • 350
  • 2
  • 7
  • That's what I am doing - updated my question. I'm thinking the code `path = r"%s" % directory` must be appending "\*.*" to the path so I can get all files ??? – dbmitch Sep 06 '16 at 23:33
  • 1
    @dbmitch `path = r"%s" % directory` cannot append anything, its just simple string interpolation. – Take_Care_ Sep 06 '16 at 23:35
  • Works fine now without that line - and specifying correct folder name with escape characters or wildcards.Thanks all – dbmitch Sep 06 '16 at 23:42
1

I don't quite understand what the [path = r"%s" % directory] line is doing

It creates a copy path of directory:

>>> directory = raw_input()
LAS Data
>>> path = r"%s" % directory

>>> path == directory
True

>>> type(path), type(directory)
(<type 'str'>, <type 'str'>)

Really wondering where you got that from. Seems pretty pointless.

Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107