AttributeError: object 'module' does not have attribute '[x]'

I am trying to make a script that moves all .txt files from the desktop to the / org desktop, the code looks like this:

import os
import shutil

userhome = os.path.expanduser('~')
src = userhome + '/Desktop/'
dst = src+ 'org/'

def main(): 
    txtlist = os.listdir(src)
    for file in txtlist:
        sortFiles(file)

def sortFiles(file):        
        if file.endswith(".txt"):
            shutil.move(src+file,dst)   


main()

If I execute .py, I get this error: AttributeError: 'module' object does not have the attribute 'copy' . However, if I delete the last line of "main ()" and I import this script as a module in the python command line and I call .main (), it works fine there. How can I make this work as a script ?

    Traceback (most recent call last):
  File "C:\Python32\org.py", line 3, in <module>
    import shutil
  File "C:\Python32\lib\shutil.py", line 14, in <module>
    import tarfile
  File "C:\Python32\lib\tarfile.py", line 50, in <module>
    import copy
  File "C:\Python32\lib\copy.py", line 61, in <module>
    from org.python.core import PyStringMap
  File "C:\Python32\org.py", line 19, in <module>
    main()
  File "C:\Python32\org.py", line 12, in main
    sortFiles(file)
  File "C:\Python32\org.py", line 16, in sortFiles
    shutil.move(src+file,dst)
AttributeError: 'module' object has no attribute 'move'

I am using python 3.2

+4
source share
1 answer

, - . , , :

Traceback (most recent call last):
  File "C:\Python32\org.py", line 3, in <module>
    import shutil

, , , import shutil. Thats, - , , .

  File "C:\Python32\lib\shutil.py", line 14, in <module>
    import tarfile
  File "C:\Python32\lib\tarfile.py", line 50, in <module
    import copy

shutil import tarfile, copy.

  File "C:\Python32\lib\copy.py", line 61, in <module>
    from org.python.core import PyStringMap

copy , PyStringMap org.python.core. , copy : PyStringMap = None.

, -, org: script, org.py. , , Python - python.core.PyStringMap org.py. , script, main() :

  File "C:\Python32\org.py", line 19, in <module>
    main()
  File "C:\Python32\org.py", line 12, in main
    sortFiles(file)
  File "C:\Python32\org.py", line 16, in sortFiles
    shutil.copy(src+file,dst)
AttributeError: 'module' object has no attribute 'copy'

shutil.copy, shutil. , ( !), , copy , AttributeError.

, script -, .

, script - .

+12

Source: https://habr.com/ru/post/1529675/


All Articles