0

For the better part of 3 months I've been slowly adding stuff in an attempt to make the system output a file to save that is human readable. I could save this all under one file but this was originally course work and has now just become so random piece of Franken-Code. So I have the program take the users family name and date of arrival and use that as the file name. So naturally the program will be writing to a file that doesn't yet exist and if it does it should add another entry as to insure bookings aren't. All I have found is people saying one of 2 things, either that it "w" or "a" should create the file or that a "+" is needed after the selection of opening type like so "a+" or "w+". I think the earlier is more likely as I think "w+" is read/write but not to sure and many different sources say different things.

#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 variables 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 variables

name = input("What is the family name --> ")
arrival = input("Please enter date of arrival in the form dd/mm/yy --> ")

while len(arrival) != 8:
    arrival = 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 arrival for latter update into the business' preferred 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 re-enter stay in weeks -->"))
if nights >=  3:
    discount_applic = 1

#Gets the guests ideal number of weeks stay, ensure that this would be possible then adds the discount if applicable
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 unachievable 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 maximum allowed by the business or the minimum (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 entries are : single, double or family --> ")
            current_room = current_room.upper()

#Ensures that the desired room is valid, if first inserted not correctly, repeats until valid entry 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 ", arrival)
print("You have booked stay for ", nights, "weeks")
print("You have booked", rooms, " room/s of these categories;")
print(str(room_list))
print("This will cost £", cost)
print("Booking referral: ", booking)

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

The error:

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ghirahim3
  • 3
  • 1
  • 6
  • can you add the error you're getting? as it's not clear what is failing here – user3012759 Oct 12 '17 at 13:41
  • There's a good list of the file modes in [this answer](https://stackoverflow.com/a/23566951/4014959) – PM 2Ring Oct 12 '17 at 13:42
  • user3012759 the error I get is as follows: Traceback (most recent call last): File "H:\Computing\S4\Programs\Suite.py", line 89, in storageFile = open(storageFileName, "a") FileNotFoundError: [Errno 2] No such file or directory: '15/10/17Daivdson.txt' – Ghirahim3 Oct 12 '17 at 13:43
  • PM 2Ring I had used "w" which should create a new file as that answer suggests but it was just spitting out the file not found error instead of creating it like the program should do, once the invevitable mountain of errors is solved. – Ghirahim3 Oct 12 '17 at 13:47
  • @Ghirahim3 so your problem is that your file contains slashes which essentially mean that it's a directory... so Python tries to create a file in given directory which obviously doesn't exits... you should format your file name differently to not use `/` and it should work – user3012759 Oct 12 '17 at 13:48
  • User3012759 I don't see where it would get a \ from? It shouldn't do it, the file name should just output as datename.txt where date and name are varibles and not just the words date and name – Ghirahim3 Oct 12 '17 at 13:51
  • 1
    @Ghirahim3 your date is `15/10/17` so your datename will be `15/10/17Daivdson` which doesn't work, if you replace `/` with say `-` in your `dateStr` it should just work https://docs.python.org/3.6/library/stdtypes.html#str.replace – user3012759 Oct 12 '17 at 13:57
  • Thanks, seems so obious now. I had to convert it from appending it to writting because it didn't like that then it yelled to change "," to "+". Now the thing won't acutally save the data after I have every intiger being saved converted to a string so I can concatinate it – Ghirahim3 Oct 12 '17 at 14:06

0 Answers0