File name with special character “^” unrecognized by pattern.match (RegEx function)












2















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")











share|improve this question























  • 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


















2















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")











share|improve this question























  • 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
















2












2








2








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")











share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 9:58









DavidDavid

214




214













  • 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





















  • 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



















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














2 Answers
2






active

oldest

votes


















4














^ 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))





share|improve this answer


























  • Thanks a lot, it works perfectly with your regex.escape function solution :-)

    – David
    Nov 26 '18 at 15:22



















1














^ 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()))





share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









4














^ 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))





share|improve this answer


























  • Thanks a lot, it works perfectly with your regex.escape function solution :-)

    – David
    Nov 26 '18 at 15:22
















4














^ 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))





share|improve this answer


























  • Thanks a lot, it works perfectly with your regex.escape function solution :-)

    – David
    Nov 26 '18 at 15:22














4












4








4







^ 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))





share|improve this answer















^ 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))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 10:28

























answered Nov 25 '18 at 10:09









Martijn PietersMartijn Pieters

719k14025102320




719k14025102320













  • 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





Thanks a lot, it works perfectly with your regex.escape function solution :-)

– David
Nov 26 '18 at 15:22













1














^ 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()))





share|improve this answer


























  • 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
















1














^ 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()))





share|improve this answer


























  • 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














1












1








1







^ 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()))





share|improve this answer















^ 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()))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 10:13

























answered Nov 25 '18 at 10:11









Tomothy32Tomothy32

7,7711628




7,7711628













  • 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











  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Marschland

Redirect URL with Chrome Remote Debugging Android Devices