File name with special character “^” unrecognized by pattern.match (RegEx function)
I'm working with Python 3.6.5 on a Winows 10.
I can not verify if a file exists in a directory or not.
The problem seems to come from the special character "^".
When I run the code below, the "WITHOUT_CIRCUMFLEX" and "^WITH_CIRCUMFLEX" files are well listed by the os.listdir () function.
However, the file "^ WITH_CIRCUMFLEX" is not recognized by the function pattern.match (file) ... while it exists!
Would anyone have an idea to solve this problem?
Thanks for your help
# coding: utf-8
import pandas as pd
import os.path
import regex
path = "C:UsersDavidtest"
list_name = ['WITHOUT_CIRCUMFLEX', '^WITH_CIRCUMFLEX']
df_empty = pd.DataFrame()
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
pattern = regex.compile('{name}.pkl'.format(**locals()))
# Check if file already exist
check = False
for file in os.listdir(path):
print("I found this filet" + file)
if pattern.match(file):
check = True
if check is True:
print("t" + name + " file exist" + "n")
else:
print("t" + name + " does not exist")
python regex utf-8
|
show 2 more comments
I'm working with Python 3.6.5 on a Winows 10.
I can not verify if a file exists in a directory or not.
The problem seems to come from the special character "^".
When I run the code below, the "WITHOUT_CIRCUMFLEX" and "^WITH_CIRCUMFLEX" files are well listed by the os.listdir () function.
However, the file "^ WITH_CIRCUMFLEX" is not recognized by the function pattern.match (file) ... while it exists!
Would anyone have an idea to solve this problem?
Thanks for your help
# coding: utf-8
import pandas as pd
import os.path
import regex
path = "C:UsersDavidtest"
list_name = ['WITHOUT_CIRCUMFLEX', '^WITH_CIRCUMFLEX']
df_empty = pd.DataFrame()
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
pattern = regex.compile('{name}.pkl'.format(**locals()))
# Check if file already exist
check = False
for file in os.listdir(path):
print("I found this filet" + file)
if pattern.match(file):
check = True
if check is True:
print("t" + name + " file exist" + "n")
else:
print("t" + name + " does not exist")
python regex utf-8
What's the regex module? I think your meaning isimport re
. And^
is a metacharacter. You have to use^
.
– user7121223
Nov 25 '18 at 10:04
Python has an inbuilt regex module, but the name is "re". to use it, type import re. I have not seen the import regex statement before.
– Paritosh Singh
Nov 25 '18 at 10:05
^
is a regex meta character, meaning "match at the start of the string". You'd need to escape it if you wanted to use it in a regex.
– Martijn Pieters♦
Nov 25 '18 at 10:06
Why use a regex for this though? Are you looking for files that end in{name}.pkl
, or files that consist entirely of{name}.pkl
? Neither match requires a regex.
– Martijn Pieters♦
Nov 25 '18 at 10:11
1
@ParitoshSingh: there are legitimate uses for theregex
module. I don't see those here, but it could very well be intentional.
– Martijn Pieters♦
Nov 25 '18 at 10:12
|
show 2 more comments
I'm working with Python 3.6.5 on a Winows 10.
I can not verify if a file exists in a directory or not.
The problem seems to come from the special character "^".
When I run the code below, the "WITHOUT_CIRCUMFLEX" and "^WITH_CIRCUMFLEX" files are well listed by the os.listdir () function.
However, the file "^ WITH_CIRCUMFLEX" is not recognized by the function pattern.match (file) ... while it exists!
Would anyone have an idea to solve this problem?
Thanks for your help
# coding: utf-8
import pandas as pd
import os.path
import regex
path = "C:UsersDavidtest"
list_name = ['WITHOUT_CIRCUMFLEX', '^WITH_CIRCUMFLEX']
df_empty = pd.DataFrame()
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
pattern = regex.compile('{name}.pkl'.format(**locals()))
# Check if file already exist
check = False
for file in os.listdir(path):
print("I found this filet" + file)
if pattern.match(file):
check = True
if check is True:
print("t" + name + " file exist" + "n")
else:
print("t" + name + " does not exist")
python regex utf-8
I'm working with Python 3.6.5 on a Winows 10.
I can not verify if a file exists in a directory or not.
The problem seems to come from the special character "^".
When I run the code below, the "WITHOUT_CIRCUMFLEX" and "^WITH_CIRCUMFLEX" files are well listed by the os.listdir () function.
However, the file "^ WITH_CIRCUMFLEX" is not recognized by the function pattern.match (file) ... while it exists!
Would anyone have an idea to solve this problem?
Thanks for your help
# coding: utf-8
import pandas as pd
import os.path
import regex
path = "C:UsersDavidtest"
list_name = ['WITHOUT_CIRCUMFLEX', '^WITH_CIRCUMFLEX']
df_empty = pd.DataFrame()
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
pattern = regex.compile('{name}.pkl'.format(**locals()))
# Check if file already exist
check = False
for file in os.listdir(path):
print("I found this filet" + file)
if pattern.match(file):
check = True
if check is True:
print("t" + name + " file exist" + "n")
else:
print("t" + name + " does not exist")
python regex utf-8
python regex utf-8
asked Nov 25 '18 at 9:58
DavidDavid
214
214
What's the regex module? I think your meaning isimport re
. And^
is a metacharacter. You have to use^
.
– user7121223
Nov 25 '18 at 10:04
Python has an inbuilt regex module, but the name is "re". to use it, type import re. I have not seen the import regex statement before.
– Paritosh Singh
Nov 25 '18 at 10:05
^
is a regex meta character, meaning "match at the start of the string". You'd need to escape it if you wanted to use it in a regex.
– Martijn Pieters♦
Nov 25 '18 at 10:06
Why use a regex for this though? Are you looking for files that end in{name}.pkl
, or files that consist entirely of{name}.pkl
? Neither match requires a regex.
– Martijn Pieters♦
Nov 25 '18 at 10:11
1
@ParitoshSingh: there are legitimate uses for theregex
module. I don't see those here, but it could very well be intentional.
– Martijn Pieters♦
Nov 25 '18 at 10:12
|
show 2 more comments
What's the regex module? I think your meaning isimport re
. And^
is a metacharacter. You have to use^
.
– user7121223
Nov 25 '18 at 10:04
Python has an inbuilt regex module, but the name is "re". to use it, type import re. I have not seen the import regex statement before.
– Paritosh Singh
Nov 25 '18 at 10:05
^
is a regex meta character, meaning "match at the start of the string". You'd need to escape it if you wanted to use it in a regex.
– Martijn Pieters♦
Nov 25 '18 at 10:06
Why use a regex for this though? Are you looking for files that end in{name}.pkl
, or files that consist entirely of{name}.pkl
? Neither match requires a regex.
– Martijn Pieters♦
Nov 25 '18 at 10:11
1
@ParitoshSingh: there are legitimate uses for theregex
module. I don't see those here, but it could very well be intentional.
– Martijn Pieters♦
Nov 25 '18 at 10:12
What's the regex module? I think your meaning is
import re
. And ^
is a metacharacter. You have to use ^
.– user7121223
Nov 25 '18 at 10:04
What's the regex module? I think your meaning is
import re
. And ^
is a metacharacter. You have to use ^
.– user7121223
Nov 25 '18 at 10:04
Python has an inbuilt regex module, but the name is "re". to use it, type import re. I have not seen the import regex statement before.
– Paritosh Singh
Nov 25 '18 at 10:05
Python has an inbuilt regex module, but the name is "re". to use it, type import re. I have not seen the import regex statement before.
– Paritosh Singh
Nov 25 '18 at 10:05
^
is a regex meta character, meaning "match at the start of the string". You'd need to escape it if you wanted to use it in a regex.– Martijn Pieters♦
Nov 25 '18 at 10:06
^
is a regex meta character, meaning "match at the start of the string". You'd need to escape it if you wanted to use it in a regex.– Martijn Pieters♦
Nov 25 '18 at 10:06
Why use a regex for this though? Are you looking for files that end in
{name}.pkl
, or files that consist entirely of {name}.pkl
? Neither match requires a regex.– Martijn Pieters♦
Nov 25 '18 at 10:11
Why use a regex for this though? Are you looking for files that end in
{name}.pkl
, or files that consist entirely of {name}.pkl
? Neither match requires a regex.– Martijn Pieters♦
Nov 25 '18 at 10:11
1
1
@ParitoshSingh: there are legitimate uses for the
regex
module. I don't see those here, but it could very well be intentional.– Martijn Pieters♦
Nov 25 '18 at 10:12
@ParitoshSingh: there are legitimate uses for the
regex
module. I don't see those here, but it could very well be intentional.– Martijn Pieters♦
Nov 25 '18 at 10:12
|
show 2 more comments
2 Answers
2
active
oldest
votes
^
is a regex meta character, so it'll not match a literal ^
character in text. You'd need to escape such characters:
'^WITH_CIRCUMFLEX'
If your inputs are generated or taken from another source, use the regex.escape()
function to escape meta characters for you:
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
name = regex.escape(name, special_only=True)
pattern = regex.compile('{name}.pkl'.format(**locals()))
However, if you are looking to match files, you currently are not using any of the reasons you'd use a regular expression. Your pattern will at best match any filename that ends with {name}.pkl
. You'd be much better off using the glob module:
import glob
for name in list_name:
...
escaped_name = glob.escape(name)
files = glob.glob('*{}.pkl'.format(escaped_name))
Thanks a lot, it works perfectly with your regex.escape function solution :-)
– David
Nov 26 '18 at 15:22
add a comment |
^
is a regex metacharacter, so you have to escape it. The easiest way to do this is to use the regex.escape
function, which automatically escapes metacharacters in an arbitrary string.
So instead of
pattern = regex.compile('{name}.pkl'.format(**locals()))
use
pattern = regex.compile(regex.escape('{name}.pkl').format(**locals()))
regex
is the next-generation version, installable from PyPI: pypi.org/project/regex
– Martijn Pieters♦
Nov 25 '18 at 10:12
Not that they are using the features of the library here.
– Martijn Pieters♦
Nov 25 '18 at 10:12
Didn't know that, I'll change my answer. Thanks!
– Tomothy32
Nov 25 '18 at 10:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466404%2ffile-name-with-special-character-unrecognized-by-pattern-match-regex-functi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
^
is a regex meta character, so it'll not match a literal ^
character in text. You'd need to escape such characters:
'^WITH_CIRCUMFLEX'
If your inputs are generated or taken from another source, use the regex.escape()
function to escape meta characters for you:
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
name = regex.escape(name, special_only=True)
pattern = regex.compile('{name}.pkl'.format(**locals()))
However, if you are looking to match files, you currently are not using any of the reasons you'd use a regular expression. Your pattern will at best match any filename that ends with {name}.pkl
. You'd be much better off using the glob module:
import glob
for name in list_name:
...
escaped_name = glob.escape(name)
files = glob.glob('*{}.pkl'.format(escaped_name))
Thanks a lot, it works perfectly with your regex.escape function solution :-)
– David
Nov 26 '18 at 15:22
add a comment |
^
is a regex meta character, so it'll not match a literal ^
character in text. You'd need to escape such characters:
'^WITH_CIRCUMFLEX'
If your inputs are generated or taken from another source, use the regex.escape()
function to escape meta characters for you:
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
name = regex.escape(name, special_only=True)
pattern = regex.compile('{name}.pkl'.format(**locals()))
However, if you are looking to match files, you currently are not using any of the reasons you'd use a regular expression. Your pattern will at best match any filename that ends with {name}.pkl
. You'd be much better off using the glob module:
import glob
for name in list_name:
...
escaped_name = glob.escape(name)
files = glob.glob('*{}.pkl'.format(escaped_name))
Thanks a lot, it works perfectly with your regex.escape function solution :-)
– David
Nov 26 '18 at 15:22
add a comment |
^
is a regex meta character, so it'll not match a literal ^
character in text. You'd need to escape such characters:
'^WITH_CIRCUMFLEX'
If your inputs are generated or taken from another source, use the regex.escape()
function to escape meta characters for you:
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
name = regex.escape(name, special_only=True)
pattern = regex.compile('{name}.pkl'.format(**locals()))
However, if you are looking to match files, you currently are not using any of the reasons you'd use a regular expression. Your pattern will at best match any filename that ends with {name}.pkl
. You'd be much better off using the glob module:
import glob
for name in list_name:
...
escaped_name = glob.escape(name)
files = glob.glob('*{}.pkl'.format(escaped_name))
^
is a regex meta character, so it'll not match a literal ^
character in text. You'd need to escape such characters:
'^WITH_CIRCUMFLEX'
If your inputs are generated or taken from another source, use the regex.escape()
function to escape meta characters for you:
for name in list_name:
df_empty.to_pickle('{path}{name}.pkl'.format(**locals()))
name = regex.escape(name, special_only=True)
pattern = regex.compile('{name}.pkl'.format(**locals()))
However, if you are looking to match files, you currently are not using any of the reasons you'd use a regular expression. Your pattern will at best match any filename that ends with {name}.pkl
. You'd be much better off using the glob module:
import glob
for name in list_name:
...
escaped_name = glob.escape(name)
files = glob.glob('*{}.pkl'.format(escaped_name))
edited Nov 25 '18 at 10:28
answered Nov 25 '18 at 10:09
Martijn Pieters♦Martijn Pieters
715k13724952308
715k13724952308
Thanks a lot, it works perfectly with your regex.escape function solution :-)
– David
Nov 26 '18 at 15:22
add a comment |
Thanks a lot, it works perfectly with your regex.escape function solution :-)
– David
Nov 26 '18 at 15:22
Thanks a lot, it works perfectly with your regex.escape function solution :-)
– David
Nov 26 '18 at 15:22
Thanks a lot, it works perfectly with your regex.escape function solution :-)
– David
Nov 26 '18 at 15:22
add a comment |
^
is a regex metacharacter, so you have to escape it. The easiest way to do this is to use the regex.escape
function, which automatically escapes metacharacters in an arbitrary string.
So instead of
pattern = regex.compile('{name}.pkl'.format(**locals()))
use
pattern = regex.compile(regex.escape('{name}.pkl').format(**locals()))
regex
is the next-generation version, installable from PyPI: pypi.org/project/regex
– Martijn Pieters♦
Nov 25 '18 at 10:12
Not that they are using the features of the library here.
– Martijn Pieters♦
Nov 25 '18 at 10:12
Didn't know that, I'll change my answer. Thanks!
– Tomothy32
Nov 25 '18 at 10:13
add a comment |
^
is a regex metacharacter, so you have to escape it. The easiest way to do this is to use the regex.escape
function, which automatically escapes metacharacters in an arbitrary string.
So instead of
pattern = regex.compile('{name}.pkl'.format(**locals()))
use
pattern = regex.compile(regex.escape('{name}.pkl').format(**locals()))
regex
is the next-generation version, installable from PyPI: pypi.org/project/regex
– Martijn Pieters♦
Nov 25 '18 at 10:12
Not that they are using the features of the library here.
– Martijn Pieters♦
Nov 25 '18 at 10:12
Didn't know that, I'll change my answer. Thanks!
– Tomothy32
Nov 25 '18 at 10:13
add a comment |
^
is a regex metacharacter, so you have to escape it. The easiest way to do this is to use the regex.escape
function, which automatically escapes metacharacters in an arbitrary string.
So instead of
pattern = regex.compile('{name}.pkl'.format(**locals()))
use
pattern = regex.compile(regex.escape('{name}.pkl').format(**locals()))
^
is a regex metacharacter, so you have to escape it. The easiest way to do this is to use the regex.escape
function, which automatically escapes metacharacters in an arbitrary string.
So instead of
pattern = regex.compile('{name}.pkl'.format(**locals()))
use
pattern = regex.compile(regex.escape('{name}.pkl').format(**locals()))
edited Nov 25 '18 at 10:13
answered Nov 25 '18 at 10:11
Tomothy32Tomothy32
7,0731626
7,0731626
regex
is the next-generation version, installable from PyPI: pypi.org/project/regex
– Martijn Pieters♦
Nov 25 '18 at 10:12
Not that they are using the features of the library here.
– Martijn Pieters♦
Nov 25 '18 at 10:12
Didn't know that, I'll change my answer. Thanks!
– Tomothy32
Nov 25 '18 at 10:13
add a comment |
regex
is the next-generation version, installable from PyPI: pypi.org/project/regex
– Martijn Pieters♦
Nov 25 '18 at 10:12
Not that they are using the features of the library here.
– Martijn Pieters♦
Nov 25 '18 at 10:12
Didn't know that, I'll change my answer. Thanks!
– Tomothy32
Nov 25 '18 at 10:13
regex
is the next-generation version, installable from PyPI: pypi.org/project/regex– Martijn Pieters♦
Nov 25 '18 at 10:12
regex
is the next-generation version, installable from PyPI: pypi.org/project/regex– Martijn Pieters♦
Nov 25 '18 at 10:12
Not that they are using the features of the library here.
– Martijn Pieters♦
Nov 25 '18 at 10:12
Not that they are using the features of the library here.
– Martijn Pieters♦
Nov 25 '18 at 10:12
Didn't know that, I'll change my answer. Thanks!
– Tomothy32
Nov 25 '18 at 10:13
Didn't know that, I'll change my answer. Thanks!
– Tomothy32
Nov 25 '18 at 10:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466404%2ffile-name-with-special-character-unrecognized-by-pattern-match-regex-functi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the regex module? I think your meaning is
import re
. And^
is a metacharacter. You have to use^
.– user7121223
Nov 25 '18 at 10:04
Python has an inbuilt regex module, but the name is "re". to use it, type import re. I have not seen the import regex statement before.
– Paritosh Singh
Nov 25 '18 at 10:05
^
is a regex meta character, meaning "match at the start of the string". You'd need to escape it if you wanted to use it in a regex.– Martijn Pieters♦
Nov 25 '18 at 10:06
Why use a regex for this though? Are you looking for files that end in
{name}.pkl
, or files that consist entirely of{name}.pkl
? Neither match requires a regex.– Martijn Pieters♦
Nov 25 '18 at 10:11
1
@ParitoshSingh: there are legitimate uses for the
regex
module. I don't see those here, but it could very well be intentional.– Martijn Pieters♦
Nov 25 '18 at 10:12