Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape space in filepath

Tags:

python

regex

I'm trying to write a python tool that will read a logfile and process it

One thing it should do is use the paths listed in the logfile (it's a logfile for a backup tool)

/Volumes/Live_Jobs/Live_Jobs/*SCANS\ and\ LE\ Docs/_LE_PROOFS_DOCS/JEM_lj/JEM/0002_OXO_CorkScrew/3\ Delivery/GG_Double\ Lever\ Waiters\ Corkscrew_072613_Mike_RETOUCHED/gg_3110200_2_V3_Final.tif

Unfortunately the paths that I'm provided with aren't appropriately escaped and I've had trouble properly escaping in python. Perhaps python isn't the best tool for this, but I like it's flexibility - it will allow me to extend whatever I write

Using the regex escape function escapes too many characters, pipes.quote method doesn't escape the spaces, and if I use a regex to replace ' ' with '\ ' I end up getting

/Volumes/Live_Jobs/Live_Jobs/*SCANS\\ and\\ LE\\ Docs/_LE_PROOFS_DOCS/JEM_lj/JEM/0002_OXO_CorkScrew/3\\ Delivery/GG_Double\\ Lever\\ Waiters\\ Corkscrew_072613_Mike_RETOUCHED/gg_3110200_2_V3_Final.tif

which are double escaped and wont pass to python functions like os.path.getsize().

What am I doing wrong??

like image 648
user2666063 Avatar asked Aug 08 '13 21:08

user2666063


People also ask

How do you escape space in file path?

In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes (""). Also try using the Grave Accent Character (`) PowerShell uses the grave accent (`) character as its escape character. Just add it before each space in the file name.

Can you have spaces in file paths?

There is no legitimate reason to avoid spaces. All modern file systems handle file names with spaces just fine.

How do you escape space in SCP command?

Basically you need to escape it twice, because it's escaped locally and then on the remote end. There are a couple of options you can do (in bash): scp [email protected]:"'web/tmp/Master File 18 10 13. xls'" .

How do you escape space in shell?

First, you can choose to type a backslash when a space is about to happen that is actually a part of the file system path. The shell will interpret that backslash as an escape of the space.


2 Answers

If you're reading paths out of a file, and passing them to functions like os.path.getsize, you don't need to escape them. For example:

>>> with open('name with spaces', 'w') as f:
...     f.write('abc\n')
>>> os.path.getsize('name with spaces')
4

In fact, there are only a handful of functions in Python that need spaces escaped, either because they're passing a string to the shell (like os.system) or because they're trying to do shell-like parsing on your behalf (like subprocess.foo with an arg string instead of an arg list).


So, let's say logfile.txt looks like this:

/Volumes/My Drive/My Scans/Batch 1/foo bar.tif
/Volumes/My Drive/My Scans/Batch 1/spam eggs.tif
/Volumes/My Drive/My Scans/Batch 2/another long name.tif

… then something like this will work fine:

with open('logfile.txt') as logf:
    for line in logf:
        with open(line.rstrip()) as f:
            do_something_with_tiff_file(f)

Noticing those * characters in your example, if these are glob patterns, that's fine too:

with open('logfile.txt') as logf:
    for line in logf:
        for path in glob.glob(line.rstrip()):
            with open(path) as f:
                do_something_with_tiff_file(f)

If your problem is the exact opposite of what you described, and the file is full of strings that are escaped, and you want to unescape them, decode('string_escape') will undo Python-style escaping, and there are different functions to undo different kinds of escaping, but without knowing what kind of escaping you want to undo it's hard to say which function you want…

like image 79
abarnert Avatar answered Sep 23 '22 02:09

abarnert


Try this:

  myfile = open(r'c:\tmp\junkpythonfile','w')

The 'r' stands for a raw string.

You could also use \ like

myfile = open('c:\\tmp\\junkpythonfile','w')
like image 27
Aakash Anuj Avatar answered Sep 21 '22 02:09

Aakash Anuj