Question

i'm very new to python, and i'm attempting to make a very rudimentary VM, with the ability to make and read "text files" (formatted strings).

now, there is going to be a command that allows the user to create a file. the file is stored as a class, but i am unsure how to do something like this -

def filecreate(filename,contents):
    filename = contents
filecreate(str(raw_input("File Name: ")), insertcontenthere)

upon attempting to do something like that in idle/pycharm, you can't, because it yells at you for the filename being undefined.

i've looked into the constructor functions, but i'm at a loss. the answer is likely obvious and i'm not seeing it, but i can't find it.

(and it's probably been answered, but because of the phrasing, i can't accurately search for it.)

Was it helpful?

Solution

You'll have to create a file system somehow or another. If I may suggest something:

class FileSystem(dict):
    # I'm not really sure what you need a file system
    # to do specifically. Probably things like
    # FileSystem.listdir but this is kind of beyond the
    # scope of this question

fs = FileSystem()

def filecreate(filename,contents):
    global fs
    fs[filename] = contents

filecreate(str(raw_input("File Name: ")), insertcontenthere)

It may not be a bad idea to implement this all into the FileSystem, so you'll instead call fs.filecreate(raw_input(), content). Also, you never need to call str on raw_input -- it will ALWAYS return a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top