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

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.

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.

Python regular expression to capture last word with missing line feed [duplicate]

This question already has answers here:
Python csv string to array
(10 answers)
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
I need to capture words separated by tabs as illustrated in the image below.
The expression (.*?)[\t|\n] works well, except for the last line where a line feed is missing. Can anyone suggest a modification of the regular expression to also match the last word, i.e. Cheyenne? Link to code example
Replace [\t|\n] with (\t|$).
BTW, [\t|\n] is a character class, so the pipe | is literal here. You probably meant [\t\n].

Python - Spaces in Filenames [duplicate]

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("\&*!")
"'\\&*!'"

Python typing "\" [duplicate]

This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 6 years ago.
I want to print a string that ends with the \ character, but the problem is it makes the following ") part of the string, so won't work. Is there any way to end a string with \ being considered a regular character?
print("somestuffhere\") and this is still part of that string...
Put another "\" character behind it. This escapes the escape character. Like so:
print("somestuffhere\\")

Categories

Resources