Python抱怨'ntpath'中没有expanduser,即使我导入了os.path

时间:2017-10-04 18:29:47

标签: python

我怎么能这样做

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'
cmd.exe

中的

但在我的脚本中不能这样做?

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'

最小脚本示例:

import os.path

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

这是在Windows 10上。不确定我可以提供哪些其他详细信息。

1 个答案:

答案 0 :(得分:1)

这是一个错字:AttributeError: module 'ntpath' has no attribute 'expanderuser'

不应该是expanduser吗?