Home > front end >  How Can I Make A Working .py File Within a Python File?
How Can I Make A Working .py File Within a Python File?

Time:12-03

I am trying to make the file py.py within this code

def makeFile():
    contents = "import JustIRC\nimport requests";
    file = open('py.py');
    file.write(contents);
    file.close();

But if I run it it returns io.UnsupportedOperation: not writable What is the problem and how can I fix it? I need to be able to specifically tab and newline for the program to run correctly

CodePudding user response:

You need to open the file with the write option:

file = open("py.py", "w")

CodePudding user response:

It depends how you want to use that file:
file = open('py.py', 'w') - create the file if it doesn't exist and write to it. If the file exists, delete everything from it and write the new content
file = open('py.py', 'a') - create the file if it doesn't exists. If the file exists, keep the content and write at the end

  • Related