Escape space in filepath

2024/10/10 10:26:46

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??

Answer

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…

https://en.xdnf.cn/q/69902.html

Related Q&A

How to save web page as text file?

I would like to save a web page (all content) as a text file. (As if you did right click on webpage -> "Save Page As" -> "Save as text file" and not as html file)I have tried …

PIL Image.size returns the opposite width/height

Using PIL to determine width and height of imagesOn a specific image (luckily only this one - but it is troubling) the width/height returning from image.size is the opposite. The image: http://storage.…

Django Rest Framework: Register multiple serializers in ViewSet

Im trying to create a custom API (not using models), but its not showing the request definition in the schema (in consequence, not showing it in swagger). My current code is:views.pyclass InfoViewSet(v…

Which is most accurate way to distinguish one of 8 colors?

Imagine we how some basic colors:RED = Color ((196, 2, 51), "RED") ORANGE = Color ((255, 165, 0), "ORANGE") YELLOW = Color ((255, 205, 0), "YELLOW") GREEN = Color ((0, 128…

Lambda function behavior with and without keyword arguments

I am using lambda functions for GUI programming with tkinter. Recently I got stuck when implementing buttons that open files: self.file="" button = Button(conf_f, text="Tools opt.",…

How to get type annotation within python function scope?

For example: def test():a: intb: strprint(__annotations__) test()This function call raises a NameError: name __annotations__ is not defined error. What I want is to get the type annotation within the f…

General explanation of how epoll works?

Im doing a technical write-up on switching from a database-polling (via synchronous stored procedure call) to a message queue (via pub/sub). Id like to be able to explain how polling a database is vas…

How to return cost, grad as tuple for scipys fmin_cg function

How can I make scipys fmin_cg use one function that returns cost and gradient as a tuple? The problem with having f for cost and fprime for gradient, is that I might have to perform an operation twice…

n-gram name analysis in non-english languages (CJK, etc)

Im working on deduping a database of people. For a first pass, Im following a basic 2-step process to avoid an O(n^2) operation over the whole database, as described in the literature. First, I "b…

How to retrieve all the attributes of LDAP database

I am using ldap module of python to connect to ldap server. I am able to query the database but I dont know how to retrieve the fields present in the database, so that I can notify the user in advance …