0

I need to create a text file and write to it, I am very new to programming all together and I would like some advice.

#part1
def writetofile():
myFile = open("C:\Andrew\myText.txt", "a")
myFile.write(a)
myFile.close()

#part2
def textfileopen():
  bottle = ['Zero','One','Two','Three','Four','Five','Six','Seven','Eight', 'Nine', 'Ten' ]
  text_one = 'green bottles hanging on the wall'
  text_two = 'And if one green bottle should accidentally fall\nThere\'ll be'




for i in range(10, 0, -1):
    print(bottle[i], text_one)
    print(bottle[i], text_one)
    print(text_two, bottle[i-1])

textfileopen()
#part3

I was given an example:

def kevin():
File = open('abc.txt','a')
File.write(a)
File.close()



def skinner ():
kevin('rabbits')

skinner()

My program is suppose to execute from the "skinner" or "textfileopen" (Am i missing a python main block?) Part 3 is suppose to execute the program and call part 2 and then part 2 calls part 1 to create the text file. I know you cannot start a program with a def, I am making mistakes with my defs I think but I can't work out what exactly i am stuffing up. I could use some advice or help thanks.

2 Answers2

0

One of your problems is that you define the filename as "C:\Andrew\myText.txt". The problem with this is that python reads "\A" as one character because of the \ (which is actually called the escape character).
You want an actual \ in your string, which you can do in one of the following two ways:

open(r"C:\Andrew\myText.txt", "a")  # notice the use of `r`, for "raw string"

OR

open("C:\\Andrew\\myText.txt", "a")  # the first \ escapes the second, to give you a \
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • 1
    While it's good to point out using raw strings when using backslashes... `\A` is not an escape char... (nor is `\m`) - there's more deep seated issues such as the function never being called and not accepting arguments for instance... – Jon Clements Dec 01 '13 at 08:52
  • Thanks for the help, I am not sure if this is one of the issues as I had to set a specific path to create the file because it was trying to write to the root directory – user3047711 Dec 01 '13 at 08:55
  • @JonClements: I was trying to explain that `'\'` is the escape character. I would appreciate a clarifying edit to that end, if you see a better way to explain it. Also, OP's post is very underspecified. For one, it doesn't explain how the observed behavior diverges from the expected. So I went with what I thought was most likely to be the error – inspectorG4dget Dec 01 '13 at 08:56
  • 1
    @inspectorG4dget At the moment, it's more of a problem that `writetofile` is never called and the whole `textfileopen` doesn't make sense (with or without the loop in it). So I believe an answer to this question requires a much more fundamental explanation of how to call functions and in what order they should be called, more than a specific issue regarding escape characters (which may happen in the future and so is worth pointing out) as the OP hasn't even reached that point :) – Jon Clements Dec 01 '13 at 09:19
  • @JonClements: Good point. I'm just going to leave my answer here... just 'coz – inspectorG4dget Dec 01 '13 at 09:20
0

Whenever possible, try & use the with statement for file handling.

Also, make function return things that are required. So that, we can use these things & the function does not have to itself manipulate stuff (which is most of the times dangerous for sensitive data)

Another python hack is the str.join() method. It is faster than all other methods & is more pythonic.

#part1
def writetofile(to_write):
    with open("C:\Andrew\myText.txt", "a") as myFile:
        myFile.write(to_write)

#part2
def textfileopen():
    bottle = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five',
              'Six', 'Seven', 'Eight', 'Nine', 'Ten' ]
    text_one = 'green bottles hanging on the wall'
    text_two = "And if one green bottle should accidentally fall\nThere'll be"

    all_lines = []
    for i in range(10, 0, -1):
        line1  = " ".join([bottle[i], text_one])
        line2  = " ".join([bottle[i], text_one])
        line34 = " ".join([text_two, bottle[i-1], text_one])
        all_lines.append("\n".join([line1, line2, line34]))
    return "".join(all_lines)

#print(textfileopen())
writetofile(textfileopen())
#part3
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90