当正在写入的文件尚未存在时,Python无法创建文件

时间:2017-10-12 13:37:40

标签: python file

在3个月的大部分时间里,我一直在慢慢添加一些东西,以使系统输出一个文件来保存,这是人类可读的。我可以将这一切保存在一个文件下,但这是正常的课程工作,现在变成了随机的Franken-Code。所以我让程序获取用户的姓氏和日期,并将其用作文件名。所以程序自然会写入一个尚不存在的文件,如果是,它应该添加另一个条目,以确保预订不是。我所发现的只是人们说两件事之一,要么“w”或“a”应该创建文件,要么在选择开头类型后需要“+”,如“a +”或“w +”。我认为早期更有可能,因为我认为“w +”是读/写但不确定,许多不同的来源说不同的东西。

#Comment Format
#
#Code
#
#Comment about above Code
from random import*
c_single = 47
c_double = 90
c_family = 250
discount = 10
VAT = 20
max_stay = 14
min_stay = 1
room_list = []
min_rooms = 1
max_rooms = 4
cost = 0

#Sets all need varibles that would need to be changed if the physical business changes (e.g. Increase in number of rooms, Longer Max stay allowed, Change to Pricing)

print("Cost of room:")
print("Single - £", c_single)
print("Double - £", c_double)
print("Family - £", c_family)

#Prints prices based on above varibles

name = input("What is the family name --> ")
arival = input("Please enter date of arival in the form dd/mm/yy --> ")
while len(arival) != 8:
    arival = input("Please re-enter, in the case of the month or day not being 2 digits long insert a 0 before to insure the form dd/mm/yy is followed --> ")
#Gets the guests name and date of arival for latter update into the business' preffered method of storage for bookings

nights = int(input("How many nights do you intend to stay in weeks --> "))
while nights > max_stay or nights < min_stay:
    nights = int(input("That is too short or long, please reneter stay in weeks -->"))
if nights >=  3:
    dicount_aplic = 1

#Gets the guests ideal number of weeks stay, ensure that this would be possible then adds the discount if aplicable
rooms = int(input("Please enter number of rooms required --> "))
while rooms < min_rooms or rooms > max_rooms:
    rooms = int(input("That number of rooms is unacheivalble in one purchase, please re-enter the number of rooms you require --> "))

#Gets number of rooms desired and checks that it does not exceed the maxium allowed by the business or the minium (currently 1, staying no nights doesn't work)

for room in range (rooms):
    current_room = input("Please enter the room desired--> ")
    current_room = current_room.upper()
    if current_room == "SINGLE":
        cost += c_single
    elif current_room == "DOUBLE":
        cost += c_double
    elif current_room == "FAMILY":
        cost += c_family

    #Checks which room is desired

    else:
        while current_room != "SINGLE" and current_room != "DOUBLE" and current_room != "FAMILY":
            current_room = input("Invalid room type, vaild enterteries are : single, double or family --> ")
            current_room = current_room.upper()

#Ensures that the desired room is valid, if first inserted not correctly, repets until valid entery is entered

room_list.append (current_room)

#Adds the wanted room type to the array room_list

cost = cost * nights
#calculates cost
booking = randrange(1000,9999)

print("Name: ", name)
print("You have booked from ", arival)
print("You have booked stay for ", nights, "weeks")
print("You have booked", rooms, " room/s of these catagories;")
print(str(room_list))
print("This will cost £", cost)
print("Booking reffernal: ", booking)

#Prints booking information
dateStr = str(arival)
storageFileName = str(dateStr + name + ".txt")
storageFile = open(storageFileName, "w")
storageFile.write("Name: ", name)
storageFile.write("They have booked from ", arival)
storageFile.write("They have booked stay for ", nights, "weeks")
storageFile.write("They have booked", rooms, " room/s of these catagories;")
storageFile.write(str(room_list))
storageFile.write("This has cost them -- >£", cost)
storageFile.write("Booking reffernal: ", booking)
#saves the storage data to a server under the name and data.

错误:

Traceback (most recent call last): 
File "H:\Computing\S4\Programs\Suite.py", line 89, 
in <module> storageFile = open(storageFileName, "w+") 
FileNotFoundError: [Errno 2] No such file or directory: '15/10/17Daivdson.txt`'

0 个答案:

没有答案