【发布时间】:2021-04-30 15:13:32
【问题描述】:
我不知道为什么会出现此错误消息...我的文件路径有问题吗?我是不是用错了操作系统?
非常感谢任何帮助!
def save_hotel_info_file(self):
hotel_name = self.name
new_hotel = (hotel_name.replace(' ', '_')).lower()
path = 'hotels/' + hotel_name
os.makedirs(path)
fobj = open('hotel_info.txt', 'w', encoding='utf-8')
fobj.write(self.name + '\n')
for room_obj in self.rooms:
room_str = 'Room ' + str(room_obj.room_num) + ',' + room_obj.room_type + ',' + str(room_obj.price)
fobj.write(room_str + '\n')
fobj.close()
```
Traceback (most recent call last):
File "/Users/myname/Documents/hotel.py", line 136, in <module>
h.save_hotel_info_file()
File "/Users/myname/Documents/hotel.py", line 120, in save_hotel_info_file
os.makedirs(path)
File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/../../../../../../../Python.framework/Versions/3.7/lib/python3.7/os.py", line 223, in makedirs
mkdir(name, mode)
FileExistsError: [Errno 17] File exists: 'hotels/Queen Elizabeth Hotel'
【问题讨论】: