AttributeError: module 'os' has no attribute 'chdir'

AttributeError: module 'os' has no attribute 'chdir'

在 Python 3 中,我只是尝试按以下方式更改我的工作目录:

import os 

DIR = 'C:/Users/Emil/files/'

os.chdir(DIR)

这曾经对我有用。但是,现在我收到错误:

AttributeError: module 'os' has no attribute 'chdir'

谁能告诉我这里出了什么问题,我该如何解决?

其他 os 模块似乎仍然可以正常工作。当我 运行:

for file in os.listdir(DIR):
    print(file)

它给了我预期的输出。

此外,当我 运行:

sys.modules['os']

输出为:

<module 'os' from 'C:\Users\20200016\AppData\Local\Continuum\anaconda3\lib\os.py'>

并且:

dir(os)

输出:

['DirEntry',
 'F_OK',
 'MutableMapping',
 'O_APPEND',
 'O_BINARY',
 'O_CREAT',
 'O_EXCL',
 'O_NOINHERIT',
 'O_RANDOM',
 'O_RDONLY',
 'O_RDWR',
 'O_SEQUENTIAL',
 'O_SHORT_LIVED',
 'O_TEMPORARY',
 'O_TEXT',
 'O_TRUNC',
 'O_WRONLY',
 'P_DETACH',
 'P_NOWAIT',
 'P_NOWAITO',
 'P_OVERLAY',
 'P_WAIT',
 'PathLike',
 'R_OK',
 'SEEK_CUR',
 'SEEK_END',
 'SEEK_SET',
 'TMP_MAX',
 'W_OK',
 'X_OK',
 '_Environ',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_execvpe',
 '_exists',
 '_exit',
 '_fspath',
 '_get_exports_list',
 '_putenv',
 '_unsetenv',
 '_wrap_close',
 'abc',
 'abort',
 'access',
 'altsep',
 'chmod',
 'close',
 'closerange',
 'cpu_count',
 'curdir',
 'defpath',
 'device_encoding',
 'devnull',
 'dup',
 'dup2',
 'environ',
 'error',
 'execl',
 'execle',
 'execlp',
 'execlpe',
 'execv',
 'execve',
 'execvp',
 'execvpe',
 'extsep',
 'fdopen',
 'fsdecode',
 'fsencode',
 'fspath',
 'fstat',
 'fsync',
 'ftruncate',
 'get_exec_path',
 'get_handle_inheritable',
 'get_inheritable',
 'get_terminal_size',
 'getcwd',
 'getcwdb',
 'getenv',
 'getlogin',
 'getpid',
 'getppid',
 'isatty',
 'kill',
 'linesep',
 'link',
 'listdir',
 'lseek',
 'lstat',
 'makedirs',
 'mkdir',
 'name',
 'open',
 'pardir',
 'path',
 'pathsep',
 'pipe',
 'popen',
 'putenv',
 'read',
 'readlink',
 'remove',
 'removedirs',
 'rename',
 'renames',
 'replace',
 'rmdir',
 'scandir',
 'sep',
 'set_handle_inheritable',
 'set_inheritable',
 'spawnl',
 'spawnle',
 'spawnv',
 'spawnve',
 'st',
 'startfile',
 'stat',
 'stat_result',
 'statvfs_result',
 'strerror',
 'supports_bytes_environ',
 'supports_dir_fd',
 'supports_effective_ids',
 'supports_fd',
 'supports_follow_symlinks',
 'symlink',
 'sys',
 'system',
 'terminal_size',
 'times',
 'times_result',
 'truncate',
 'umask',
 'uname_result',
 'unlink',
 'urandom',
 'utime',
 'waitpid',
 'walk',
 'write']

重新启动我的 IDE 后,我可以再次使用 os.chdir

现在它可以工作了,我有以下内容:

sys.modules['os']



<module 'os' from 'C:\Users\20200016\AppData\Local\Continuum\anaconda3\lib\os.py'>