python-3.7 - 属性错误 : module 'os' has no attribute 'getcwd' ?

标签 python-3.7

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

   //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 时遇到的错误。
    > "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: ''

最佳答案

这是一个老问题,但有人可能有同样的问题。我还收到以下错误:AttributeError: module 'os' has no attribute 'getcwd'。这是因为文件名为 os.py。更改文件名后问题解决。

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

关于python-3.7 - 属性错误 : module 'os' has no attribute 'getcwd' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56491440/

相关文章:

python - 为什么 python 不显示在路径图标中?

python-3.7 - 无法在 python3.7-alpine docker 镜像上安装钟摆

python - 如何只打印字典中的一项

python - 使用 Array<Map<String,String>> 列读取 Parquet 文件

python - 在 python 3.7 中使用 paho mqtt 客户端的问题

Python 3.6 DateTime Strptime 返回错误,而 Python 3.7 运行良好

python - 收到 pyserial 错误 "TypeError: ' >' not supported between instances of ' 字节' 和 'int'

python - 无法在PyCharm Mac OS 10.13中安装turicreate

Django:通过ID获取最后一条记录[多对一关系]

opencv - conda python 3.7 opencv 导入错误 libgnutls.so.30 : undefined symbol: mpn_add_1, 版本 HOGWEED_4