gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:28:19 31 4
gpt4 key购买 nike

我正在尝试运行一个 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com