首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:模块'os‘没有属性'getcwd'?

AttributeError:模块'os‘没有属性'getcwd'?
EN

Stack Overflow用户
提问于 2019-06-07 17:16:15
回答 2查看 8.1K关注 0票数 4

我正在尝试运行一个python程序,该程序从用户那里获取输入,如目录名、文件名及其内容,然后我尝试在当前路径中使用文件创建目录,然后将数据写入其中,但我得到一个错误,即"os没有mkdir属性“。

代码语言:javascript
复制
   //Code

     import os

        print(os.getcwd())

        class createfile:
            __name=''
            __file_name=''
            __input_text=''
            def read_and_create(self):
                name = input("Enter the directory name, you want to create: ")
                os.mkdir(self.__name)
                #sdos.mkdir(self.__name)
                self.__file_name=input("Enter the file name, you want to create: ")
                f=open(self.___file_name,'w')
                self.__input_text=input("Enter the text : ")
                f.write(self.__input_text)
                f.close()

            def read_file_content(self):
                x=open(self.__file_name,'r')
                print(x.read())


        o=createfile()
        o.read_and_create()
        o.read_file_content()

这是我使用Pycharm得到的错误。

代码语言:javascript
复制
    > "C:\Users\Aryan
    > Kumar\AppData\Local\Programs\Python\Python37-32\python.exe"
    > "C:/Users/Aryan Kumar/Desktop/0/os.py"
    >     Fatal Python error: initsite: Failed to import the site module
    >     Traceback (most recent call last):
    >       File "C:\Users\Aryan Kumar\AppData\Local\Programs\Python\Python37-32\lib\site.py", line 73,
    > in <module>
    >         import os
    >       File "C:\Users\Aryan Kumar\Desktop\0\os.py", line 2, in <module>
    >         print(os.getcwd())
    >     AttributeError: module 'os' has no attribute 'getcwd'
    >     
    >     Process finished with exit code 1


this is the error i got using Spyder ide in anaconda environment

runfile('C:/Users/Aryan Kumar/Desktop/0/untitled0.py', wdir='C:/Users/Aryan Kumar/Desktop/0')
    C:\Users\Aryan Kumar\Desktop\0

    Enter the directory name, you want to create: aac
    Traceback (most recent call last):

      File "<ipython-input-4-fb1c179833e1>", line 1, in <module>
        runfile('C:/Users/Aryan Kumar/Desktop/0/untitled0.py', wdir='C:/Users/Aryan Kumar/Desktop/0')

      File "C:\Users\Aryan Kumar\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 709, in runfile
        execfile(filename, namespace)

      File "C:\Users\Aryan Kumar\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
        exec(compile(f.read(), filename, 'exec'), namespace)

      File "C:/Users/Aryan Kumar/Desktop/0/untitled0.py", line 32, in <module>
        o.read_and_create()

      File "C:/Users/Aryan Kumar/Desktop/0/untitled0.py", line 18, in read_and_create
        os.mkdir(self.__name)

    FileNotFoundError: [WinError 3] The system cannot find the path specified: ''
EN

回答 2

Stack Overflow用户

发布于 2019-12-26 03:22:17

这是一个古老的问题,但有些人可能也有同样的问题。我还收到以下错误: AttributeError:模块'os‘没有'getcwd’属性。这是因为文件名为os.py。更改文件名后解决的问题。

我还发现在不同的问题中也提供了类似的答案:https://stackoverflow.com/a/43565408/298160

票数 1
EN

Stack Overflow用户

发布于 2020-07-06 22:51:55

在Python3中使用os.curdir()而不是os.cwd()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56491440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档