Click here to Skip to main content
14,330,121 members
Rate this:
Please Sign up or sign in to vote.
At the bottom of my code, below the class, it says I have an invalid syntax for "text" on the right of function.createtextfile() = create "text". I put in quotation marks around text so you guys would know where it says that. This is my code

print("If you would like to create a new text file type 'create text', If you would like to add text type 'add text', If you would like to read your file type 'read text'")
class function:
    def createtextfile():
        text4 = '\nHello there'
        text2 = open('c:/Users/Me/Desktop/Victor.txt','w')
        text2.write(text)
        text2.close
    def addtext():
        text3 = open('c:/Users/Me/Desktop/Victor.txt','a')
        something = input('what would you like to add? ')
        text3.write(something)
        text3.close
    def readtext():
        justread = open('c:/Users/Me/Desktop/Victor.txt','r').read()
        print(justread)
        
function.createtextfile() = create text
function.addtext() = add text
function.readtext() = read text


What I have tried:

I have tried to put parentheses in front of "text" but that didn't do anything.
Posted
Updated 14-Sep-19 6:46am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

That syntax is wrong in two ways. Firstly your text strings are not enclosed in quotes. And secondly you cannot set a function equal to some value.

See 6. Modules — Python 3.4.8 documentation[^] for how to define and call functions correctly.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Do you mean something similar to
class function:
    def createtextfile(self, text):
        f = open('foo.txt','w')
        f.write(text)
        f.close()
    def addtext(self):
        f = open('foo.txt','a')
        something = input('what would you like to add? ')
        f.write(something)
        f.close()
    def readtext(self):
        justread = open('foo.txt','r').read()
        print(justread)


f = function()
f.createtextfile("foo\n")
f.addtext()
f.readtext()
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100