Absolute path of string that contains special characters
In my code, I get a path from the database that may contain special escaping characters that I need to convert them to a real path name. I'm using python 3.7 on Windows.
Suppose this path: C:Files2c2b25410025test.x
IMPORTANT: the path is not a fixed value in the code and it is an output of executing a Stored Procedure from pyodbc.
When I try to convert it to an absolute path I get this error:
ValueError: _getfullpathname: embedded null character in path
I also tried to replace "" with "/" but with no luck.
import os
# path = cursor.execute(query, "some_input").fetchone()[0]
path = 'C:Files2c2b25410025test.x'
print(os.path.abspath(path))
python
add a comment |
In my code, I get a path from the database that may contain special escaping characters that I need to convert them to a real path name. I'm using python 3.7 on Windows.
Suppose this path: C:Files2c2b25410025test.x
IMPORTANT: the path is not a fixed value in the code and it is an output of executing a Stored Procedure from pyodbc.
When I try to convert it to an absolute path I get this error:
ValueError: _getfullpathname: embedded null character in path
I also tried to replace "" with "/" but with no luck.
import os
# path = cursor.execute(query, "some_input").fetchone()[0]
path = 'C:Files2c2b25410025test.x'
print(os.path.abspath(path))
python
1
I have updated my answer and wrote a function that will give you the correct path. You can modify that based on your requirement. Please check and let me know in comment if it does not satisfy your requirement. Anyway, I learned a lot from your problem. Great, thank you.
– hygull
Nov 24 '18 at 7:04
add a comment |
In my code, I get a path from the database that may contain special escaping characters that I need to convert them to a real path name. I'm using python 3.7 on Windows.
Suppose this path: C:Files2c2b25410025test.x
IMPORTANT: the path is not a fixed value in the code and it is an output of executing a Stored Procedure from pyodbc.
When I try to convert it to an absolute path I get this error:
ValueError: _getfullpathname: embedded null character in path
I also tried to replace "" with "/" but with no luck.
import os
# path = cursor.execute(query, "some_input").fetchone()[0]
path = 'C:Files2c2b25410025test.x'
print(os.path.abspath(path))
python
In my code, I get a path from the database that may contain special escaping characters that I need to convert them to a real path name. I'm using python 3.7 on Windows.
Suppose this path: C:Files2c2b25410025test.x
IMPORTANT: the path is not a fixed value in the code and it is an output of executing a Stored Procedure from pyodbc.
When I try to convert it to an absolute path I get this error:
ValueError: _getfullpathname: embedded null character in path
I also tried to replace "" with "/" but with no luck.
import os
# path = cursor.execute(query, "some_input").fetchone()[0]
path = 'C:Files2c2b25410025test.x'
print(os.path.abspath(path))
python
python
edited Nov 24 '18 at 6:35
Masoud
asked Nov 24 '18 at 6:13
MasoudMasoud
5841629
5841629
1
I have updated my answer and wrote a function that will give you the correct path. You can modify that based on your requirement. Please check and let me know in comment if it does not satisfy your requirement. Anyway, I learned a lot from your problem. Great, thank you.
– hygull
Nov 24 '18 at 7:04
add a comment |
1
I have updated my answer and wrote a function that will give you the correct path. You can modify that based on your requirement. Please check and let me know in comment if it does not satisfy your requirement. Anyway, I learned a lot from your problem. Great, thank you.
– hygull
Nov 24 '18 at 7:04
1
1
I have updated my answer and wrote a function that will give you the correct path. You can modify that based on your requirement. Please check and let me know in comment if it does not satisfy your requirement. Anyway, I learned a lot from your problem. Great, thank you.
– hygull
Nov 24 '18 at 7:04
I have updated my answer and wrote a function that will give you the correct path. You can modify that based on your requirement. Please check and let me know in comment if it does not satisfy your requirement. Anyway, I learned a lot from your problem. Great, thank you.
– hygull
Nov 24 '18 at 7:04
add a comment |
4 Answers
4
active
oldest
votes
Judging by your comments on the other answers, it sounds like the data is already corrupted in the database you're using. That is, you have a literal null byte stored there, and perhaps other bogus bytes (like 2 perhaps turning into x02). So you probably need two fixes.
First, you should fix whatever code is putting values into the database, so it won't put bogus data in any more. You haven't described how the data gets into the database, so I we can't give you much guidance on how to do this. But most programming languages (and DB libraries) have tools to prevent escape sequences from being evaluated in strings where they're not wanted.
Once you've stopped new bad data from getting added, you can work on fixing the values that are already in the database. It probably shouldn't be too hard to write a query that will replace null bytes with \0 (or whatever the appropriate escape sequence is for your DB). You may want to look for special characters like newlines (n) and unprintable characters (like x02) as well.
I'd only try to fix this issue on the output end if you don't have any control of the database at all.
Of course, it can be fixed on the database side, but I was wondering if python can handle this somehow. BTW, the output is a function on SQL server that returns path of an uploaded file in the database itself. So are you saying that python is unable to handle this?
– Masoud
Nov 24 '18 at 7:45
1
Well, I don't know anything about your database specifically, but that sounds to me like there's a misconfiguration or bug in the library you use to access the database. There's no way it should be evaluating the escape sequences in a path it produces itself if its doing anything sane. You might want to ask a more specific question about your database code to get help with that, rather than trying to fix the output in Python. There are a lot of escape sequences it might be messing up, and since some of them might be valid in a path, it may be hard to tell which ones are errors.
– Blckknght
Nov 24 '18 at 8:02
add a comment |
I think the below is the right way to solve your problem.
>>> def get_fixed_path(path):
... path = repr(path)
... path = path.replace("\", "\\")
... path = path.replace("\x", "\\0")
... path = os.path.abspath(path3).split("'")[1]
... return path
...
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
>>> final_path = get_fixed_path(path)
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
And here is the detailed description of each and every steps/statements in the above solution.
First step (problem)
>>> import os
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
Second step (problem)
>>> path2 = repr(path)
>>> path2
"'C:\\Files\x02c2b2541\x0025\test.x'"
>>>
>>> print(path2)
'C:\Filesx02c2b2541x0025test.x'
>>>
Third step (problem)
>>> path3 = path2.replace("\", "\\")
>>> path3
"'C:\\\\Files\\x02c2b2541\\x0025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\x02c2b2541\x0025\test.x'
>>>
>>> path3 = path3.replace("\x", "\\0")
>>> path3
"'C:\\\\Files\\\002c2b2541\\\00025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\02c2b2541\0025\test.x'
>>>
Fourth step (problem)
>>> os.path.abspath(path3)
"C:\Users\RISHIKESH\'C:\Files\002c2b2541\00025\test.x'"
>>>
>>> os.path.abspath(path2)
"C:\Users\RISHIKESH\'C:\Files\x02c2b2541\x0025\test.x'"
>>>
>>> os.path.abspath('k')
'C:\Users\RISHIKESH\k'
>>>
>>> os.path.abspath(path3).split("'")
['C:\Users\RISHIKESH\', 'C:\Files\002c2b2541\00025\test.x', '']
>>> os.path.abspath(path3).split("'")[1]
'C:\Files\002c2b2541\00025\test.x'
>>>
Final step (solution)
>>> final_path = os.path.abspath(path3).split("'")[1]
>>>
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
downvoted because the OP said he got the string from a database; he isn't assigning the string himself.
– Dale
Nov 24 '18 at 6:21
@Dale, I respect your comment and appreciate down voting me to point my mistake. Now I've updated my answer. Thank you.
– hygull
Nov 24 '18 at 7:01
1
I like your method but you are replacing corrupted data based on the input, how about this path:C:Files5c2b25410025test.xI getC:Files05c2b25410025test.xon output.
– Masoud
Nov 24 '18 at 7:49
1
Ok got, extra zeroes problem in005c2b2541. Let me solve and I'll let you here in comment. Thank you.
– hygull
Nov 24 '18 at 7:51
add a comment |
Replace "" by "\".
That's it.
if you're using double quotes it needs to be:s = s.replace("\", "\\")
– Dale
Nov 24 '18 at 6:20
Thanks for your remind, but I think he got my idea :D
– Lê Tư Thành
Nov 24 '18 at 6:22
I still get the same error.
– Masoud
Nov 24 '18 at 6:28
add a comment |
You need to either use a raw string literal or double backslashes \.
import os
path = r'C:Files2c2b25410025test.x' #r before the string
print(os.path.abspath(path))
Thanks but I get the path from the output of a database query so it is not a fixed value.
– Masoud
Nov 24 '18 at 6:21
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%2f53455660%2fabsolute-path-of-string-that-contains-special-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Judging by your comments on the other answers, it sounds like the data is already corrupted in the database you're using. That is, you have a literal null byte stored there, and perhaps other bogus bytes (like 2 perhaps turning into x02). So you probably need two fixes.
First, you should fix whatever code is putting values into the database, so it won't put bogus data in any more. You haven't described how the data gets into the database, so I we can't give you much guidance on how to do this. But most programming languages (and DB libraries) have tools to prevent escape sequences from being evaluated in strings where they're not wanted.
Once you've stopped new bad data from getting added, you can work on fixing the values that are already in the database. It probably shouldn't be too hard to write a query that will replace null bytes with \0 (or whatever the appropriate escape sequence is for your DB). You may want to look for special characters like newlines (n) and unprintable characters (like x02) as well.
I'd only try to fix this issue on the output end if you don't have any control of the database at all.
Of course, it can be fixed on the database side, but I was wondering if python can handle this somehow. BTW, the output is a function on SQL server that returns path of an uploaded file in the database itself. So are you saying that python is unable to handle this?
– Masoud
Nov 24 '18 at 7:45
1
Well, I don't know anything about your database specifically, but that sounds to me like there's a misconfiguration or bug in the library you use to access the database. There's no way it should be evaluating the escape sequences in a path it produces itself if its doing anything sane. You might want to ask a more specific question about your database code to get help with that, rather than trying to fix the output in Python. There are a lot of escape sequences it might be messing up, and since some of them might be valid in a path, it may be hard to tell which ones are errors.
– Blckknght
Nov 24 '18 at 8:02
add a comment |
Judging by your comments on the other answers, it sounds like the data is already corrupted in the database you're using. That is, you have a literal null byte stored there, and perhaps other bogus bytes (like 2 perhaps turning into x02). So you probably need two fixes.
First, you should fix whatever code is putting values into the database, so it won't put bogus data in any more. You haven't described how the data gets into the database, so I we can't give you much guidance on how to do this. But most programming languages (and DB libraries) have tools to prevent escape sequences from being evaluated in strings where they're not wanted.
Once you've stopped new bad data from getting added, you can work on fixing the values that are already in the database. It probably shouldn't be too hard to write a query that will replace null bytes with \0 (or whatever the appropriate escape sequence is for your DB). You may want to look for special characters like newlines (n) and unprintable characters (like x02) as well.
I'd only try to fix this issue on the output end if you don't have any control of the database at all.
Of course, it can be fixed on the database side, but I was wondering if python can handle this somehow. BTW, the output is a function on SQL server that returns path of an uploaded file in the database itself. So are you saying that python is unable to handle this?
– Masoud
Nov 24 '18 at 7:45
1
Well, I don't know anything about your database specifically, but that sounds to me like there's a misconfiguration or bug in the library you use to access the database. There's no way it should be evaluating the escape sequences in a path it produces itself if its doing anything sane. You might want to ask a more specific question about your database code to get help with that, rather than trying to fix the output in Python. There are a lot of escape sequences it might be messing up, and since some of them might be valid in a path, it may be hard to tell which ones are errors.
– Blckknght
Nov 24 '18 at 8:02
add a comment |
Judging by your comments on the other answers, it sounds like the data is already corrupted in the database you're using. That is, you have a literal null byte stored there, and perhaps other bogus bytes (like 2 perhaps turning into x02). So you probably need two fixes.
First, you should fix whatever code is putting values into the database, so it won't put bogus data in any more. You haven't described how the data gets into the database, so I we can't give you much guidance on how to do this. But most programming languages (and DB libraries) have tools to prevent escape sequences from being evaluated in strings where they're not wanted.
Once you've stopped new bad data from getting added, you can work on fixing the values that are already in the database. It probably shouldn't be too hard to write a query that will replace null bytes with \0 (or whatever the appropriate escape sequence is for your DB). You may want to look for special characters like newlines (n) and unprintable characters (like x02) as well.
I'd only try to fix this issue on the output end if you don't have any control of the database at all.
Judging by your comments on the other answers, it sounds like the data is already corrupted in the database you're using. That is, you have a literal null byte stored there, and perhaps other bogus bytes (like 2 perhaps turning into x02). So you probably need two fixes.
First, you should fix whatever code is putting values into the database, so it won't put bogus data in any more. You haven't described how the data gets into the database, so I we can't give you much guidance on how to do this. But most programming languages (and DB libraries) have tools to prevent escape sequences from being evaluated in strings where they're not wanted.
Once you've stopped new bad data from getting added, you can work on fixing the values that are already in the database. It probably shouldn't be too hard to write a query that will replace null bytes with \0 (or whatever the appropriate escape sequence is for your DB). You may want to look for special characters like newlines (n) and unprintable characters (like x02) as well.
I'd only try to fix this issue on the output end if you don't have any control of the database at all.
answered Nov 24 '18 at 7:19
BlckknghtBlckknght
63.4k557103
63.4k557103
Of course, it can be fixed on the database side, but I was wondering if python can handle this somehow. BTW, the output is a function on SQL server that returns path of an uploaded file in the database itself. So are you saying that python is unable to handle this?
– Masoud
Nov 24 '18 at 7:45
1
Well, I don't know anything about your database specifically, but that sounds to me like there's a misconfiguration or bug in the library you use to access the database. There's no way it should be evaluating the escape sequences in a path it produces itself if its doing anything sane. You might want to ask a more specific question about your database code to get help with that, rather than trying to fix the output in Python. There are a lot of escape sequences it might be messing up, and since some of them might be valid in a path, it may be hard to tell which ones are errors.
– Blckknght
Nov 24 '18 at 8:02
add a comment |
Of course, it can be fixed on the database side, but I was wondering if python can handle this somehow. BTW, the output is a function on SQL server that returns path of an uploaded file in the database itself. So are you saying that python is unable to handle this?
– Masoud
Nov 24 '18 at 7:45
1
Well, I don't know anything about your database specifically, but that sounds to me like there's a misconfiguration or bug in the library you use to access the database. There's no way it should be evaluating the escape sequences in a path it produces itself if its doing anything sane. You might want to ask a more specific question about your database code to get help with that, rather than trying to fix the output in Python. There are a lot of escape sequences it might be messing up, and since some of them might be valid in a path, it may be hard to tell which ones are errors.
– Blckknght
Nov 24 '18 at 8:02
Of course, it can be fixed on the database side, but I was wondering if python can handle this somehow. BTW, the output is a function on SQL server that returns path of an uploaded file in the database itself. So are you saying that python is unable to handle this?
– Masoud
Nov 24 '18 at 7:45
Of course, it can be fixed on the database side, but I was wondering if python can handle this somehow. BTW, the output is a function on SQL server that returns path of an uploaded file in the database itself. So are you saying that python is unable to handle this?
– Masoud
Nov 24 '18 at 7:45
1
1
Well, I don't know anything about your database specifically, but that sounds to me like there's a misconfiguration or bug in the library you use to access the database. There's no way it should be evaluating the escape sequences in a path it produces itself if its doing anything sane. You might want to ask a more specific question about your database code to get help with that, rather than trying to fix the output in Python. There are a lot of escape sequences it might be messing up, and since some of them might be valid in a path, it may be hard to tell which ones are errors.
– Blckknght
Nov 24 '18 at 8:02
Well, I don't know anything about your database specifically, but that sounds to me like there's a misconfiguration or bug in the library you use to access the database. There's no way it should be evaluating the escape sequences in a path it produces itself if its doing anything sane. You might want to ask a more specific question about your database code to get help with that, rather than trying to fix the output in Python. There are a lot of escape sequences it might be messing up, and since some of them might be valid in a path, it may be hard to tell which ones are errors.
– Blckknght
Nov 24 '18 at 8:02
add a comment |
I think the below is the right way to solve your problem.
>>> def get_fixed_path(path):
... path = repr(path)
... path = path.replace("\", "\\")
... path = path.replace("\x", "\\0")
... path = os.path.abspath(path3).split("'")[1]
... return path
...
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
>>> final_path = get_fixed_path(path)
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
And here is the detailed description of each and every steps/statements in the above solution.
First step (problem)
>>> import os
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
Second step (problem)
>>> path2 = repr(path)
>>> path2
"'C:\\Files\x02c2b2541\x0025\test.x'"
>>>
>>> print(path2)
'C:\Filesx02c2b2541x0025test.x'
>>>
Third step (problem)
>>> path3 = path2.replace("\", "\\")
>>> path3
"'C:\\\\Files\\x02c2b2541\\x0025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\x02c2b2541\x0025\test.x'
>>>
>>> path3 = path3.replace("\x", "\\0")
>>> path3
"'C:\\\\Files\\\002c2b2541\\\00025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\02c2b2541\0025\test.x'
>>>
Fourth step (problem)
>>> os.path.abspath(path3)
"C:\Users\RISHIKESH\'C:\Files\002c2b2541\00025\test.x'"
>>>
>>> os.path.abspath(path2)
"C:\Users\RISHIKESH\'C:\Files\x02c2b2541\x0025\test.x'"
>>>
>>> os.path.abspath('k')
'C:\Users\RISHIKESH\k'
>>>
>>> os.path.abspath(path3).split("'")
['C:\Users\RISHIKESH\', 'C:\Files\002c2b2541\00025\test.x', '']
>>> os.path.abspath(path3).split("'")[1]
'C:\Files\002c2b2541\00025\test.x'
>>>
Final step (solution)
>>> final_path = os.path.abspath(path3).split("'")[1]
>>>
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
downvoted because the OP said he got the string from a database; he isn't assigning the string himself.
– Dale
Nov 24 '18 at 6:21
@Dale, I respect your comment and appreciate down voting me to point my mistake. Now I've updated my answer. Thank you.
– hygull
Nov 24 '18 at 7:01
1
I like your method but you are replacing corrupted data based on the input, how about this path:C:Files5c2b25410025test.xI getC:Files05c2b25410025test.xon output.
– Masoud
Nov 24 '18 at 7:49
1
Ok got, extra zeroes problem in005c2b2541. Let me solve and I'll let you here in comment. Thank you.
– hygull
Nov 24 '18 at 7:51
add a comment |
I think the below is the right way to solve your problem.
>>> def get_fixed_path(path):
... path = repr(path)
... path = path.replace("\", "\\")
... path = path.replace("\x", "\\0")
... path = os.path.abspath(path3).split("'")[1]
... return path
...
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
>>> final_path = get_fixed_path(path)
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
And here is the detailed description of each and every steps/statements in the above solution.
First step (problem)
>>> import os
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
Second step (problem)
>>> path2 = repr(path)
>>> path2
"'C:\\Files\x02c2b2541\x0025\test.x'"
>>>
>>> print(path2)
'C:\Filesx02c2b2541x0025test.x'
>>>
Third step (problem)
>>> path3 = path2.replace("\", "\\")
>>> path3
"'C:\\\\Files\\x02c2b2541\\x0025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\x02c2b2541\x0025\test.x'
>>>
>>> path3 = path3.replace("\x", "\\0")
>>> path3
"'C:\\\\Files\\\002c2b2541\\\00025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\02c2b2541\0025\test.x'
>>>
Fourth step (problem)
>>> os.path.abspath(path3)
"C:\Users\RISHIKESH\'C:\Files\002c2b2541\00025\test.x'"
>>>
>>> os.path.abspath(path2)
"C:\Users\RISHIKESH\'C:\Files\x02c2b2541\x0025\test.x'"
>>>
>>> os.path.abspath('k')
'C:\Users\RISHIKESH\k'
>>>
>>> os.path.abspath(path3).split("'")
['C:\Users\RISHIKESH\', 'C:\Files\002c2b2541\00025\test.x', '']
>>> os.path.abspath(path3).split("'")[1]
'C:\Files\002c2b2541\00025\test.x'
>>>
Final step (solution)
>>> final_path = os.path.abspath(path3).split("'")[1]
>>>
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
downvoted because the OP said he got the string from a database; he isn't assigning the string himself.
– Dale
Nov 24 '18 at 6:21
@Dale, I respect your comment and appreciate down voting me to point my mistake. Now I've updated my answer. Thank you.
– hygull
Nov 24 '18 at 7:01
1
I like your method but you are replacing corrupted data based on the input, how about this path:C:Files5c2b25410025test.xI getC:Files05c2b25410025test.xon output.
– Masoud
Nov 24 '18 at 7:49
1
Ok got, extra zeroes problem in005c2b2541. Let me solve and I'll let you here in comment. Thank you.
– hygull
Nov 24 '18 at 7:51
add a comment |
I think the below is the right way to solve your problem.
>>> def get_fixed_path(path):
... path = repr(path)
... path = path.replace("\", "\\")
... path = path.replace("\x", "\\0")
... path = os.path.abspath(path3).split("'")[1]
... return path
...
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
>>> final_path = get_fixed_path(path)
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
And here is the detailed description of each and every steps/statements in the above solution.
First step (problem)
>>> import os
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
Second step (problem)
>>> path2 = repr(path)
>>> path2
"'C:\\Files\x02c2b2541\x0025\test.x'"
>>>
>>> print(path2)
'C:\Filesx02c2b2541x0025test.x'
>>>
Third step (problem)
>>> path3 = path2.replace("\", "\\")
>>> path3
"'C:\\\\Files\\x02c2b2541\\x0025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\x02c2b2541\x0025\test.x'
>>>
>>> path3 = path3.replace("\x", "\\0")
>>> path3
"'C:\\\\Files\\\002c2b2541\\\00025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\02c2b2541\0025\test.x'
>>>
Fourth step (problem)
>>> os.path.abspath(path3)
"C:\Users\RISHIKESH\'C:\Files\002c2b2541\00025\test.x'"
>>>
>>> os.path.abspath(path2)
"C:\Users\RISHIKESH\'C:\Files\x02c2b2541\x0025\test.x'"
>>>
>>> os.path.abspath('k')
'C:\Users\RISHIKESH\k'
>>>
>>> os.path.abspath(path3).split("'")
['C:\Users\RISHIKESH\', 'C:\Files\002c2b2541\00025\test.x', '']
>>> os.path.abspath(path3).split("'")[1]
'C:\Files\002c2b2541\00025\test.x'
>>>
Final step (solution)
>>> final_path = os.path.abspath(path3).split("'")[1]
>>>
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
I think the below is the right way to solve your problem.
>>> def get_fixed_path(path):
... path = repr(path)
... path = path.replace("\", "\\")
... path = path.replace("\x", "\\0")
... path = os.path.abspath(path3).split("'")[1]
... return path
...
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
>>> final_path = get_fixed_path(path)
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
And here is the detailed description of each and every steps/statements in the above solution.
First step (problem)
>>> import os
>>>
>>> path = 'C:Files2c2b25410025test.x'
>>> path
'C:\Filesx02c2b2541x0025test.x'
>>>
>>> print(path)
C:Filesc2b2541 25 est.x
>>>
Second step (problem)
>>> path2 = repr(path)
>>> path2
"'C:\\Files\x02c2b2541\x0025\test.x'"
>>>
>>> print(path2)
'C:\Filesx02c2b2541x0025test.x'
>>>
Third step (problem)
>>> path3 = path2.replace("\", "\\")
>>> path3
"'C:\\\\Files\\x02c2b2541\\x0025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\x02c2b2541\x0025\test.x'
>>>
>>> path3 = path3.replace("\x", "\\0")
>>> path3
"'C:\\\\Files\\\002c2b2541\\\00025\\test.x'"
>>>
>>> print(path3)
'C:\\Files\02c2b2541\0025\test.x'
>>>
Fourth step (problem)
>>> os.path.abspath(path3)
"C:\Users\RISHIKESH\'C:\Files\002c2b2541\00025\test.x'"
>>>
>>> os.path.abspath(path2)
"C:\Users\RISHIKESH\'C:\Files\x02c2b2541\x0025\test.x'"
>>>
>>> os.path.abspath('k')
'C:\Users\RISHIKESH\k'
>>>
>>> os.path.abspath(path3).split("'")
['C:\Users\RISHIKESH\', 'C:\Files\002c2b2541\00025\test.x', '']
>>> os.path.abspath(path3).split("'")[1]
'C:\Files\002c2b2541\00025\test.x'
>>>
Final step (solution)
>>> final_path = os.path.abspath(path3).split("'")[1]
>>>
>>> final_path
'C:\Files\002c2b2541\00025\test.x'
>>>
>>> print(final_path)
C:Files02c2b25410025test.x
>>>
edited Nov 24 '18 at 6:59
answered Nov 24 '18 at 6:18
hygullhygull
3,67021431
3,67021431
downvoted because the OP said he got the string from a database; he isn't assigning the string himself.
– Dale
Nov 24 '18 at 6:21
@Dale, I respect your comment and appreciate down voting me to point my mistake. Now I've updated my answer. Thank you.
– hygull
Nov 24 '18 at 7:01
1
I like your method but you are replacing corrupted data based on the input, how about this path:C:Files5c2b25410025test.xI getC:Files05c2b25410025test.xon output.
– Masoud
Nov 24 '18 at 7:49
1
Ok got, extra zeroes problem in005c2b2541. Let me solve and I'll let you here in comment. Thank you.
– hygull
Nov 24 '18 at 7:51
add a comment |
downvoted because the OP said he got the string from a database; he isn't assigning the string himself.
– Dale
Nov 24 '18 at 6:21
@Dale, I respect your comment and appreciate down voting me to point my mistake. Now I've updated my answer. Thank you.
– hygull
Nov 24 '18 at 7:01
1
I like your method but you are replacing corrupted data based on the input, how about this path:C:Files5c2b25410025test.xI getC:Files05c2b25410025test.xon output.
– Masoud
Nov 24 '18 at 7:49
1
Ok got, extra zeroes problem in005c2b2541. Let me solve and I'll let you here in comment. Thank you.
– hygull
Nov 24 '18 at 7:51
downvoted because the OP said he got the string from a database; he isn't assigning the string himself.
– Dale
Nov 24 '18 at 6:21
downvoted because the OP said he got the string from a database; he isn't assigning the string himself.
– Dale
Nov 24 '18 at 6:21
@Dale, I respect your comment and appreciate down voting me to point my mistake. Now I've updated my answer. Thank you.
– hygull
Nov 24 '18 at 7:01
@Dale, I respect your comment and appreciate down voting me to point my mistake. Now I've updated my answer. Thank you.
– hygull
Nov 24 '18 at 7:01
1
1
I like your method but you are replacing corrupted data based on the input, how about this path:
C:Files5c2b25410025test.x I get C:Files05c2b25410025test.x on output.– Masoud
Nov 24 '18 at 7:49
I like your method but you are replacing corrupted data based on the input, how about this path:
C:Files5c2b25410025test.x I get C:Files05c2b25410025test.x on output.– Masoud
Nov 24 '18 at 7:49
1
1
Ok got, extra zeroes problem in
005c2b2541. Let me solve and I'll let you here in comment. Thank you.– hygull
Nov 24 '18 at 7:51
Ok got, extra zeroes problem in
005c2b2541. Let me solve and I'll let you here in comment. Thank you.– hygull
Nov 24 '18 at 7:51
add a comment |
Replace "" by "\".
That's it.
if you're using double quotes it needs to be:s = s.replace("\", "\\")
– Dale
Nov 24 '18 at 6:20
Thanks for your remind, but I think he got my idea :D
– Lê Tư Thành
Nov 24 '18 at 6:22
I still get the same error.
– Masoud
Nov 24 '18 at 6:28
add a comment |
Replace "" by "\".
That's it.
if you're using double quotes it needs to be:s = s.replace("\", "\\")
– Dale
Nov 24 '18 at 6:20
Thanks for your remind, but I think he got my idea :D
– Lê Tư Thành
Nov 24 '18 at 6:22
I still get the same error.
– Masoud
Nov 24 '18 at 6:28
add a comment |
Replace "" by "\".
That's it.
Replace "" by "\".
That's it.
answered Nov 24 '18 at 6:16
Lê Tư ThànhLê Tư Thành
81110
81110
if you're using double quotes it needs to be:s = s.replace("\", "\\")
– Dale
Nov 24 '18 at 6:20
Thanks for your remind, but I think he got my idea :D
– Lê Tư Thành
Nov 24 '18 at 6:22
I still get the same error.
– Masoud
Nov 24 '18 at 6:28
add a comment |
if you're using double quotes it needs to be:s = s.replace("\", "\\")
– Dale
Nov 24 '18 at 6:20
Thanks for your remind, but I think he got my idea :D
– Lê Tư Thành
Nov 24 '18 at 6:22
I still get the same error.
– Masoud
Nov 24 '18 at 6:28
if you're using double quotes it needs to be:
s = s.replace("\", "\\")– Dale
Nov 24 '18 at 6:20
if you're using double quotes it needs to be:
s = s.replace("\", "\\")– Dale
Nov 24 '18 at 6:20
Thanks for your remind, but I think he got my idea :D
– Lê Tư Thành
Nov 24 '18 at 6:22
Thanks for your remind, but I think he got my idea :D
– Lê Tư Thành
Nov 24 '18 at 6:22
I still get the same error.
– Masoud
Nov 24 '18 at 6:28
I still get the same error.
– Masoud
Nov 24 '18 at 6:28
add a comment |
You need to either use a raw string literal or double backslashes \.
import os
path = r'C:Files2c2b25410025test.x' #r before the string
print(os.path.abspath(path))
Thanks but I get the path from the output of a database query so it is not a fixed value.
– Masoud
Nov 24 '18 at 6:21
add a comment |
You need to either use a raw string literal or double backslashes \.
import os
path = r'C:Files2c2b25410025test.x' #r before the string
print(os.path.abspath(path))
Thanks but I get the path from the output of a database query so it is not a fixed value.
– Masoud
Nov 24 '18 at 6:21
add a comment |
You need to either use a raw string literal or double backslashes \.
import os
path = r'C:Files2c2b25410025test.x' #r before the string
print(os.path.abspath(path))
You need to either use a raw string literal or double backslashes \.
import os
path = r'C:Files2c2b25410025test.x' #r before the string
print(os.path.abspath(path))
answered Nov 24 '18 at 6:14
BernardLBernardL
2,37111130
2,37111130
Thanks but I get the path from the output of a database query so it is not a fixed value.
– Masoud
Nov 24 '18 at 6:21
add a comment |
Thanks but I get the path from the output of a database query so it is not a fixed value.
– Masoud
Nov 24 '18 at 6:21
Thanks but I get the path from the output of a database query so it is not a fixed value.
– Masoud
Nov 24 '18 at 6:21
Thanks but I get the path from the output of a database query so it is not a fixed value.
– Masoud
Nov 24 '18 at 6:21
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%2f53455660%2fabsolute-path-of-string-that-contains-special-characters%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
1
I have updated my answer and wrote a function that will give you the correct path. You can modify that based on your requirement. Please check and let me know in comment if it does not satisfy your requirement. Anyway, I learned a lot from your problem. Great, thank you.
– hygull
Nov 24 '18 at 7:04