Python - Spaces in Filenames [duplicate] - python

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to escape os.system() calls in Python?
Is there a Python method of making filenames safe (ie. putting \ infront of spaces and escaping ( , ), symbols) programatically in Python?

Spaces are already "safe" for Python in open(). As for os.system() and similar functions, use subprocess instead.

>>> import pipes
>>> pipes.quote("\&*!")
"'\\&*!'"

Related

Omitting metacharacters in python [duplicate]

This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed last year.
I want to assing a path to a variable a:
a = "D:\misc\testsets\Real"
How can i omit the \t metacharacter without changing the folder name?
Use raw strings:
a = r"D:\misc\testsets\Real"
Try this:
r denotes raw string.
a = r"D:\misc\testsets\Real"

import sys showing paths with double backslash [duplicate]

This question already has answers here:
How to get rid of double backslash in python windows file path string? [duplicate]
(5 answers)
Why do backslashes appear twice?
(2 answers)
Closed 2 years ago.
I am using Spyder when I import the environmental variables using:
import sys
print(sys.path)
I get the following:
['C:\\Users\\james', 'C:\\Python\\Anaconda3\\python37.zip', 'C:\\Python\\Anaconda3\\DLLs', 'C:\\Python\\Anaconda3\\lib', 'C:\\Python\\Anaconda3', '', 'C:\\Python\\Anaconda3\\lib\\site-packages', 'C:\\Python\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Python\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Python\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Python\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\james\\.ipython']
I am wondering why I get double backslashes, while the tutorial I am watching displays the paths with a single forward slash. eg
['C:/Users/james', ...
The difference is that your tutorial is on not-windows system, and directories are like this/is/path. On Windows, the directories are like this\is\path. But in Python and in most programming languages, \(backslash) is used for escapes, so to writethis\is\path you need to write 2 slashes.

Why I cannot print the character "\" in Python [duplicate]

This question already has answers here:
How can I print a single backslash?
(4 answers)
Closed 2 years ago.
I use print() function to use like this:print("\")and i get an exception. Tell me how to slove it. Thks
Use \\ instead.
Actually, \ is a special character and you have to escape it.
print("\\") # print a single "\" character
Use:
print("\\")
instead of print("\")

How to suppress * as a wildcard, instead interpret as character? [duplicate]

This question already has answers here:
Prevent expansion of wildcards in non-quoted python script argument when running in UNIX environment
(5 answers)
Closed 3 years ago.
I am working with files that have an asterisk in the filenames.
I have a list of certain ones I am interested in but whenever I loop through them, the "*" in the name is interpreted as a wildcard.
For example: filenames are A*01:02 and A*02.
I only want to extract A*02 but the "*" is interpreted as a wildcard and both A*01:02 and A*02 are used.
How can I suppress the wildcard "*" and interpret it as a character instead?
Quote "A*02" or use a backslash as in A\*02.

Python3: f-string {} symbols screening [duplicate]

This question already has an answer here:
How to escape curly-brackets in f-strings? [duplicate]
(1 answer)
Closed 3 years ago.
i want to use f-string in my python code to write some json, but got a problem with screening {} symbols. What the correct way to screen lonely { } symbols? For example i want to write:
data = f'{[{{prod_id},{quantity},{size}}]}'
I cant get how to write it correctly, and basic python screening not working for me for some reason here.
You can escape curly braces in f-strings by doubling them, e.g.
data = f"{{[{{{prod_id},{quantity},{size}}}]}}"

Categories

Resources