Script using shutil Issue

Hi, guys,

I want to check if in a directory there is file named “TEST” and if yes, to copy it in the same directory under “TST2” name.

import os
import shutil

dstfile = 'C:\\Applications\\TST2'
path='C:\\Applications\\'
files = os.listdir(path)
for file in files:
    if "TEST" in file:
       
        srcfile = path + file
        shutil.copy(srcfile, dstfile)```



But I get this error:"AttributeError: 'str' object has no attribute 'isdir''

Any hints? Thank you!

You’re not showing us your full code, as I don’t see an isdir anywhere in what you’ve provided, but that is what is causing you the issue. Somewhere, you have some string, or more likely a variable with a string value, that you are calling .isdir on.

No, this is the full test code. What I did not show, is the full error text:

  File "<input>", line 13, in <module>
  File "C:\Users\DimitarV\.ignition\cache\gw192.168.***.***_8088\C0\pylib\shutil.py", line 117, in copy
    if os.path.isdir(dst):
AttributeError: 'str' object has no attribute 'isdir'

Ah I see, you are actually causing the error inside of shutil.py with the arguments you are passing. Do you have any package names or either os or path? Looks like shutil.py is trying to call os.path.isdir from import os, but seems like something overwrote it so that os.path is somehow just a string object instead of what it normally is.

What do you see if you print srcfile, dstfile right before you call shutil.copy

Better yet, in script console, if you can do

import os
print str(type(os.path))

do you see <type 'str'> or do you see <type 'module'>?

I see "<type ‘str’>
"

Yea that’s not good, when I do mine I get module which is wht you should expect since it is one.

The string itself is just an empty string? Or is there something in it? I only say that because if there’s some value in it, you can probably Control+F to find that string and the offending package that you have. Whta do you see from print str(os.path)?

Do you have a library script you called os by chance?

1 Like

This is what I see only:

image

I do not have library script or any script calling os modue.

Thats what you see if you do print str(type(os.path)), but what if you do print str(os.path)?

Something is wrong with the namespace of the modules that much is for sure, now its about figuring out what is being imported instead of the standard os library.

‘file’ is a keyword. Reset the Script Console, and try using a different object name.

1 Like

D’oh. That did ring alarm bells in my head but I couldn’t place it. The interactive script module highlights keywords like file, in, not, None, all of which should never be used as variable names otherwise you will run into unexpected problems. Try @JordanCClark’s suggestion and see if that fixes things.

Also are you trying to move just files or directories as well?

Not moving files and directories, just copy and rename a file that match a given name , in the same directory.

1 Like

Taking a step back, if you do

import os
print str(type(os))

Do you see

<type 'module'>
or something closer to this
<type 'com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleModule'>

I see :

image

Anyway, I did what Jordan said, and now it works. Thank you!!!

1 Like

Yea makes sense. Glad it was resolved. Generally speaking, if a word is highlighted in a special color, don’t use it as a variable name as it’s a reserved python keyword.

1 Like