简体   繁体   中英

Python complains of no expanduser in 'ntpath' even though I import os.path

How come I can do this

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os.path
>>> print(os.path)
<module 'ntpath' from 'C:\\Users\\odin\\AppData\\Local\\Programs\\Python\\Python35\\lib\\ntpath.py'>
>>> os.path.expanduser('~')
'C:\\Users\\odin'

in cmd.exe

but can't do the same in my script?

Traceback (most recent call last):
  File "C:\Users\odin\DirectoryMonitorScript\a.py", line 94, in <module>
    main()
  File "C:\Users\odin\DirectoryMonitorScript\a.py", line 52, in main
    with open(os.path.join(os.path.expanderuser('~'),'DirectoryMonitorScript','log','ScriptLog.txt'),'a') as f:
AttributeError: module 'ntpath' has no attribute 'expanderuser'

Minimal script example:

import os.path

with open(os.path.join(os.path.expanderuser('~'),'DirectoryMonitorScript','log','ScriptLog.txt'),'a') as f:
    # Do nothing
    print('Nothing...')

This is on Windows 10. Not sure what other details I could provide.

Is that a typo: AttributeError: module 'ntpath' has no attribute 'expanderuser'

Shouldn't it be expanduser ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM