1

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.)

Seki
  • 11,135
  • 7
  • 46
  • 70
ajazz
  • 83
  • 1
  • 7
  • 1
    Maybe you should explain in english what you want that code to actually do. – roippi May 02 '14 at 19:33
  • the user enters a command to make the file via raw_input, then the program makes a new object with the file class. – ajazz May 02 '14 at 19:34
  • He needs a file system, that's all. he's trying to get user input for a filename then assign the contents of the file to that file name as a variable. He can do that, but needs a file system as a dictionary to store it in. – Adam Smith May 02 '14 at 19:34
  • Adam Smith got it right. That's basically it. – ajazz May 02 '14 at 19:34

1 Answers1

1

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.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • actually, in reality, that'd be more "power-saving" and faster than making an new file class for every single one, right? – ajazz May 02 '14 at 19:37
  • 1
    I'd make filecreate an instance method of FileSystem. – dano May 02 '14 at 19:37
  • @dano I was just editing that in! Writing a file system without designing it first isn't exactly my forte! :) – Adam Smith May 02 '14 at 19:38
  • @user3348394 I'd definitely suggest creating a `class File` that contains the relevant info as well. Store the `File` instance inside your `FileSystem` at the appropriate location and call it good. – Adam Smith May 02 '14 at 19:39