Justcurious
Justcurious

Reputation: 2064

os.path AttributeError: 'str' object has no attribute 'exists'

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:\Users\test.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?

Upvotes: 7

Views: 53695

Answers (3)

Prison Mike
Prison Mike

Reputation: 51

try

os.path.exists('C:\\Users\\test.txt')

that actually works for me... i dont if it works for you

Upvotes: 0

DDGG
DDGG

Reputation: 1241

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

Upvotes: 7

Leonardo Ramos Duarte
Leonardo Ramos Duarte

Reputation: 292

Type on shell

Type(path)

And check result and value, maybe you redefine this import to a variable str.

Upvotes: 2

Related Questions