os.path AttributeError: 'str' object has no attribute 'exists'How do I check whether a file exists without exceptions?How to sort a list of objects based on an attribute of the objects?How to know if an object has an attribute in PythonCheck if a given key already exists in a dictionaryDetermine the type of an object?AttributeError: 'module' object has no attribute 'urlopen'How to find if directory exists in PythonAttributeError: 'str' object has no attribute 'write'AttributeError: 'str' object has no attribute 'set' in tkinterGetting a TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile
Strong empirical falsification of quantum mechanics based on vacuum energy density
Mixing PEX brands
Can a Canadian Travel to the USA twice, less than 180 days each time?
What is Cash Advance APR?
Open a doc from terminal, but not by its name
Can the US President recognize Israel’s sovereignty over the Golan Heights for the USA or does that need an act of Congress?
How to fade a semiplane defined by line?
Extract more than nine arguments that occur periodically in a sentence to use in macros in order to typset
What is the highest possible scrabble score for placing a single tile
Why Shazam when there is already Superman?
What are the advantages of simplicial model categories over non-simplicial ones?
Keeping a ball lost forever
putting logo on same line but after title, latex
Using substitution ciphers to generate new alphabets in a novel
Is aluminum electrical wire used on aircraft?
Biological Blimps: Propulsion
How can I write humor as character trait?
Limits and Infinite Integration by Parts
What should you do if you miss a job interview (deliberately)?
15% tax on $7.5k earnings. Is that right?
How to cover method return statement in Apex Class?
Can I visit Japan without a visa?
On a tidally locked planet, would time be quantized?
Fear of getting stuck on one programming language / technology that is not used in my country
os.path AttributeError: 'str' object has no attribute 'exists'
How do I check whether a file exists without exceptions?How to sort a list of objects based on an attribute of the objects?How to know if an object has an attribute in PythonCheck if a given key already exists in a dictionaryDetermine the type of an object?AttributeError: 'module' object has no attribute 'urlopen'How to find if directory exists in PythonAttributeError: 'str' object has no attribute 'write'AttributeError: 'str' object has no attribute 'set' in tkinterGetting a TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile
I'm trying to reproduce the code from this site: https://www.guru99.com/python-copy-file.html
The general idea is to copy a file using python. Although I can work my way around the errors, I also want to understand what I'm doing wrong in this case.
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
If used with the full directory (main('C:Userstest.txt')) The code returns the error AttributeError: 'str' object has no attribute 'exists'
. If I remove the line with path.exists()
I get a similar error: AttributeError: 'str' object has no attribute 'realpath'
.
By using the filename main('test.txt')
everything works, as long as the file is in the same folder as the python script that contains the function.
So I tried reading the docs, which states for both path.exists()
and path.realpath()
:
Changed in version 3.6: Accepts a path-like object.
Since I'm running 3.7.1 I went foward to check what is a "path-like object":
An object representing a file system path. A path-like object is either a str or bytes object representing a path, or an object implementing the os.PathLike protocol. An object that supports the os.PathLike protocol can be converted to a str or bytes file system path by calling the os.fspath() function; os.fsdecode() and os.fsencode() can be used to guarantee a str or bytes result instead, respectively. Introduced by PEP 519.
From that, given that I provided a string, I take it should be working. So what I'm missing?
python
add a comment |
I'm trying to reproduce the code from this site: https://www.guru99.com/python-copy-file.html
The general idea is to copy a file using python. Although I can work my way around the errors, I also want to understand what I'm doing wrong in this case.
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
If used with the full directory (main('C:Userstest.txt')) The code returns the error AttributeError: 'str' object has no attribute 'exists'
. If I remove the line with path.exists()
I get a similar error: AttributeError: 'str' object has no attribute 'realpath'
.
By using the filename main('test.txt')
everything works, as long as the file is in the same folder as the python script that contains the function.
So I tried reading the docs, which states for both path.exists()
and path.realpath()
:
Changed in version 3.6: Accepts a path-like object.
Since I'm running 3.7.1 I went foward to check what is a "path-like object":
An object representing a file system path. A path-like object is either a str or bytes object representing a path, or an object implementing the os.PathLike protocol. An object that supports the os.PathLike protocol can be converted to a str or bytes file system path by calling the os.fspath() function; os.fsdecode() and os.fsencode() can be used to guarantee a str or bytes result instead, respectively. Introduced by PEP 519.
From that, given that I provided a string, I take it should be working. So what I'm missing?
python
3
That doesn't sound like it has anything to do with what string you're passing in. That sounds like you named the argumentpath
in one version of your code and completely forgot about it.
– user2357112
Mar 8 at 2:19
1
@user2357112 Agreed. Since the code here is fine, voting to close as a problem that can't be reproduced.
– Joseph Sible
Mar 8 at 2:20
Tryprint(type(path))
just ahead of the first use. I expect that will producestr
rather than apath
object. If so, you need to trace the definition ofpath
back to its source. Your posted code does not show this problem.
– Prune
Mar 8 at 2:29
add a comment |
I'm trying to reproduce the code from this site: https://www.guru99.com/python-copy-file.html
The general idea is to copy a file using python. Although I can work my way around the errors, I also want to understand what I'm doing wrong in this case.
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
If used with the full directory (main('C:Userstest.txt')) The code returns the error AttributeError: 'str' object has no attribute 'exists'
. If I remove the line with path.exists()
I get a similar error: AttributeError: 'str' object has no attribute 'realpath'
.
By using the filename main('test.txt')
everything works, as long as the file is in the same folder as the python script that contains the function.
So I tried reading the docs, which states for both path.exists()
and path.realpath()
:
Changed in version 3.6: Accepts a path-like object.
Since I'm running 3.7.1 I went foward to check what is a "path-like object":
An object representing a file system path. A path-like object is either a str or bytes object representing a path, or an object implementing the os.PathLike protocol. An object that supports the os.PathLike protocol can be converted to a str or bytes file system path by calling the os.fspath() function; os.fsdecode() and os.fsencode() can be used to guarantee a str or bytes result instead, respectively. Introduced by PEP 519.
From that, given that I provided a string, I take it should be working. So what I'm missing?
python
I'm trying to reproduce the code from this site: https://www.guru99.com/python-copy-file.html
The general idea is to copy a file using python. Although I can work my way around the errors, I also want to understand what I'm doing wrong in this case.
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
If used with the full directory (main('C:Userstest.txt')) The code returns the error AttributeError: 'str' object has no attribute 'exists'
. If I remove the line with path.exists()
I get a similar error: AttributeError: 'str' object has no attribute 'realpath'
.
By using the filename main('test.txt')
everything works, as long as the file is in the same folder as the python script that contains the function.
So I tried reading the docs, which states for both path.exists()
and path.realpath()
:
Changed in version 3.6: Accepts a path-like object.
Since I'm running 3.7.1 I went foward to check what is a "path-like object":
An object representing a file system path. A path-like object is either a str or bytes object representing a path, or an object implementing the os.PathLike protocol. An object that supports the os.PathLike protocol can be converted to a str or bytes file system path by calling the os.fspath() function; os.fsdecode() and os.fsencode() can be used to guarantee a str or bytes result instead, respectively. Introduced by PEP 519.
From that, given that I provided a string, I take it should be working. So what I'm missing?
python
python
asked Mar 8 at 2:17
ZeCariocaZeCarioca
195
195
3
That doesn't sound like it has anything to do with what string you're passing in. That sounds like you named the argumentpath
in one version of your code and completely forgot about it.
– user2357112
Mar 8 at 2:19
1
@user2357112 Agreed. Since the code here is fine, voting to close as a problem that can't be reproduced.
– Joseph Sible
Mar 8 at 2:20
Tryprint(type(path))
just ahead of the first use. I expect that will producestr
rather than apath
object. If so, you need to trace the definition ofpath
back to its source. Your posted code does not show this problem.
– Prune
Mar 8 at 2:29
add a comment |
3
That doesn't sound like it has anything to do with what string you're passing in. That sounds like you named the argumentpath
in one version of your code and completely forgot about it.
– user2357112
Mar 8 at 2:19
1
@user2357112 Agreed. Since the code here is fine, voting to close as a problem that can't be reproduced.
– Joseph Sible
Mar 8 at 2:20
Tryprint(type(path))
just ahead of the first use. I expect that will producestr
rather than apath
object. If so, you need to trace the definition ofpath
back to its source. Your posted code does not show this problem.
– Prune
Mar 8 at 2:29
3
3
That doesn't sound like it has anything to do with what string you're passing in. That sounds like you named the argument
path
in one version of your code and completely forgot about it.– user2357112
Mar 8 at 2:19
That doesn't sound like it has anything to do with what string you're passing in. That sounds like you named the argument
path
in one version of your code and completely forgot about it.– user2357112
Mar 8 at 2:19
1
1
@user2357112 Agreed. Since the code here is fine, voting to close as a problem that can't be reproduced.
– Joseph Sible
Mar 8 at 2:20
@user2357112 Agreed. Since the code here is fine, voting to close as a problem that can't be reproduced.
– Joseph Sible
Mar 8 at 2:20
Try
print(type(path))
just ahead of the first use. I expect that will produce str
rather than a path
object. If so, you need to trace the definition of path
back to its source. Your posted code does not show this problem.– Prune
Mar 8 at 2:29
Try
print(type(path))
just ahead of the first use. I expect that will produce str
rather than a path
object. If so, you need to trace the definition of path
back to its source. Your posted code does not show this problem.– Prune
Mar 8 at 2:29
add a comment |
2 Answers
2
active
oldest
votes
Your code:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
It works fine, but if you re-define a local variable named path, like this:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
# The path variable here overrides the path in the main function.
path = 'abc'
main('C:\Users\test.txt') # This raises the error
This is just your code I guess, obviously this is a wrong example.
I would recommend using the os.path
after import os
, because the path variable name is very common, it is easy to conflict.
For a good example:
import shutil
import os
def main(filename):
if os.path.exists(filename):
src = os.path.realpath(filename)
head, tail = os.path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
main('C:\Users\test.txt')
main('test.txt')
is this an answer or are you showing what not to do? because shadowing a name you just imported is a horrible idea.
– Corey Goldberg
Mar 8 at 3:25
@Corey Goldberg I am showing the cause of the problem.
– DDGG
Mar 8 at 3:49
then your answer is very unclear
– Corey Goldberg
Mar 8 at 3:57
@Corey Goldberg Ok, I edited my answer.
– DDGG
Mar 8 at 6:02
add a comment |
Type on shell
Type(path)
And check result and value, maybe you redefine this import to a variable str.
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%2f55055792%2fos-path-attributeerror-str-object-has-no-attribute-exists%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
Your code:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
It works fine, but if you re-define a local variable named path, like this:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
# The path variable here overrides the path in the main function.
path = 'abc'
main('C:\Users\test.txt') # This raises the error
This is just your code I guess, obviously this is a wrong example.
I would recommend using the os.path
after import os
, because the path variable name is very common, it is easy to conflict.
For a good example:
import shutil
import os
def main(filename):
if os.path.exists(filename):
src = os.path.realpath(filename)
head, tail = os.path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
main('C:\Users\test.txt')
main('test.txt')
is this an answer or are you showing what not to do? because shadowing a name you just imported is a horrible idea.
– Corey Goldberg
Mar 8 at 3:25
@Corey Goldberg I am showing the cause of the problem.
– DDGG
Mar 8 at 3:49
then your answer is very unclear
– Corey Goldberg
Mar 8 at 3:57
@Corey Goldberg Ok, I edited my answer.
– DDGG
Mar 8 at 6:02
add a comment |
Your code:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
It works fine, but if you re-define a local variable named path, like this:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
# The path variable here overrides the path in the main function.
path = 'abc'
main('C:\Users\test.txt') # This raises the error
This is just your code I guess, obviously this is a wrong example.
I would recommend using the os.path
after import os
, because the path variable name is very common, it is easy to conflict.
For a good example:
import shutil
import os
def main(filename):
if os.path.exists(filename):
src = os.path.realpath(filename)
head, tail = os.path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
main('C:\Users\test.txt')
main('test.txt')
is this an answer or are you showing what not to do? because shadowing a name you just imported is a horrible idea.
– Corey Goldberg
Mar 8 at 3:25
@Corey Goldberg I am showing the cause of the problem.
– DDGG
Mar 8 at 3:49
then your answer is very unclear
– Corey Goldberg
Mar 8 at 3:57
@Corey Goldberg Ok, I edited my answer.
– DDGG
Mar 8 at 6:02
add a comment |
Your code:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
It works fine, but if you re-define a local variable named path, like this:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
# The path variable here overrides the path in the main function.
path = 'abc'
main('C:\Users\test.txt') # This raises the error
This is just your code I guess, obviously this is a wrong example.
I would recommend using the os.path
after import os
, because the path variable name is very common, it is easy to conflict.
For a good example:
import shutil
import os
def main(filename):
if os.path.exists(filename):
src = os.path.realpath(filename)
head, tail = os.path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
main('C:\Users\test.txt')
main('test.txt')
Your code:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\Users\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
It works fine, but if you re-define a local variable named path, like this:
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
# The path variable here overrides the path in the main function.
path = 'abc'
main('C:\Users\test.txt') # This raises the error
This is just your code I guess, obviously this is a wrong example.
I would recommend using the os.path
after import os
, because the path variable name is very common, it is easy to conflict.
For a good example:
import shutil
import os
def main(filename):
if os.path.exists(filename):
src = os.path.realpath(filename)
head, tail = os.path.split(src)
dst = src + ".bak"
shutil.copy(src, dst)
main('C:\Users\test.txt')
main('test.txt')
edited Mar 8 at 6:02
answered Mar 8 at 2:31
DDGGDDGG
406213
406213
is this an answer or are you showing what not to do? because shadowing a name you just imported is a horrible idea.
– Corey Goldberg
Mar 8 at 3:25
@Corey Goldberg I am showing the cause of the problem.
– DDGG
Mar 8 at 3:49
then your answer is very unclear
– Corey Goldberg
Mar 8 at 3:57
@Corey Goldberg Ok, I edited my answer.
– DDGG
Mar 8 at 6:02
add a comment |
is this an answer or are you showing what not to do? because shadowing a name you just imported is a horrible idea.
– Corey Goldberg
Mar 8 at 3:25
@Corey Goldberg I am showing the cause of the problem.
– DDGG
Mar 8 at 3:49
then your answer is very unclear
– Corey Goldberg
Mar 8 at 3:57
@Corey Goldberg Ok, I edited my answer.
– DDGG
Mar 8 at 6:02
is this an answer or are you showing what not to do? because shadowing a name you just imported is a horrible idea.
– Corey Goldberg
Mar 8 at 3:25
is this an answer or are you showing what not to do? because shadowing a name you just imported is a horrible idea.
– Corey Goldberg
Mar 8 at 3:25
@Corey Goldberg I am showing the cause of the problem.
– DDGG
Mar 8 at 3:49
@Corey Goldberg I am showing the cause of the problem.
– DDGG
Mar 8 at 3:49
then your answer is very unclear
– Corey Goldberg
Mar 8 at 3:57
then your answer is very unclear
– Corey Goldberg
Mar 8 at 3:57
@Corey Goldberg Ok, I edited my answer.
– DDGG
Mar 8 at 6:02
@Corey Goldberg Ok, I edited my answer.
– DDGG
Mar 8 at 6:02
add a comment |
Type on shell
Type(path)
And check result and value, maybe you redefine this import to a variable str.
add a comment |
Type on shell
Type(path)
And check result and value, maybe you redefine this import to a variable str.
add a comment |
Type on shell
Type(path)
And check result and value, maybe you redefine this import to a variable str.
Type on shell
Type(path)
And check result and value, maybe you redefine this import to a variable str.
answered Mar 8 at 2:39
Leonardo Ramos DuarteLeonardo Ramos Duarte
220310
220310
add a comment |
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%2f55055792%2fos-path-attributeerror-str-object-has-no-attribute-exists%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
3
That doesn't sound like it has anything to do with what string you're passing in. That sounds like you named the argument
path
in one version of your code and completely forgot about it.– user2357112
Mar 8 at 2:19
1
@user2357112 Agreed. Since the code here is fine, voting to close as a problem that can't be reproduced.
– Joseph Sible
Mar 8 at 2:20
Try
print(type(path))
just ahead of the first use. I expect that will producestr
rather than apath
object. If so, you need to trace the definition ofpath
back to its source. Your posted code does not show this problem.– Prune
Mar 8 at 2:29