question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

paths.py - replacing space with slash+space

See original GitHub issue

Hi!

First of all, thanks for the library. Very useful! Second, I wanted to ask you why do you replace space with slash+space in paths.py? Is this a bug or something you’ve done on purpose?

imagePath = os.path.join(rootDir, filename).replace(" ", "\\ ")

PS: I’m using Windows and Python3.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jrosebr1commented, Mar 27, 2018

It’s not a bug. It’s to escape file paths that have spaces in them. If you’re on a Unix machine try to auto-tab complete a file path that has spaces in them.

It will change:

$ cd Foo Bar

To:

$ cd Foo\ Bar

1reaction
skypanthercommented, Apr 3, 2018

There’s no reason to manually add the slash in there with that replace operation. os.path handles that for you automatically. For example, this works fine (assuming the directory and file exist):

import os

file_path = os.path.join('.', 'folder name with spaces', 'somefile.txt')

if not os.path.isfile(file_path):
    raise ValueError(f'Cannot read file at {file_path}')
with open(file_path) as f:
    print(f.read())

results in

≫ python file_test.py
this is some file

I don’t have a Windows computer to test that on, but according to the docs it should be fine. If you were targeting only python 3.4+ you could use the pathlib module instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replacing everything with a backslash till next white space
which I want to remove from my string. I am using the regular expression based replace function in Python: udpated = re.sub(r'/\fs\d ...
Read more >
How To Deal With Spaces in Paths on Mac and Linux - Medium
Method 2: Escaping Spaces With Backslash. A second, more “hardcore” method to ignore spaces uses what are called escape characters. Escape ...
Read more >
Replace Forward Slash With Backslash in Python - Codeigo
A forward-slash (/) in a Python string can be replaced by a backslash (\) using the String replace() function, translate() method, ...
Read more >
Sed replace space with backslash space (Bash) - Super User
Save this question. Show activity on this post. I'm trying to replace every space in this text file with a backslash space.
Read more >
Replace space with new line - Unix & Linux Stack Exchange
Use the tr command echo "/path/to/file /path/to/file2 /path/to/file3 /path/to/file4 /path/to/file5" \ | tr " " "\n".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found