Looks like you forgot the mode parameter when calling open, try w:
with open("copy.txt", "w") as file:
file.write("Your text goes here")
The default value is r and will fail if the file does not exist
'r' open for reading (default)
'w' open for writing, truncating the file first
Other interesting options are
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
See Doc for Python 2.7 or Python 3.6
Answer from Bentaye on Stack OverflowGenerate python code from a generic template
How to create a new text file using Python - Stack Overflow
Create a python file using cmd in windows - Stack Overflow
How to write a .py file from an existing python script? - Stack Overflow
Videos
Hi,
I need to write a console wizard that will ask several questions, and based on the answers and/or input, it will generate a py file.
For example:
$ python3 wizard.py
Name? IntelBP
Country? USA
Writing file ... Include.py generated.
$ cat include.py
name='IntelBP' country='USA'
... EOF.
So basically, what i need is a wizard engine that asks questions, and based on the answers, it reads a template (python code) and fills in some variables.
So there's essentially 2 aspects to this question:
What package can I use for writing the console interface? Ideally it will support text, lists, single, and multiple values for a specific wizard question.
Are there any packages designed to populate the values from the template into a python file? My greater concern is security related issues such as code injection.
Thanks!!
Looks like you forgot the mode parameter when calling open, try w:
with open("copy.txt", "w") as file:
file.write("Your text goes here")
The default value is r and will fail if the file does not exist
'r' open for reading (default)
'w' open for writing, truncating the file first
Other interesting options are
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
See Doc for Python 2.7 or Python 3.6
# Method 1
f = open("Path/To/Your/File.txt", "w") # 'r' for reading and 'w' for writing
f.write("Hello World from " + f.name) # Write inside file
f.close() # Close file
# Method 2
with open("Path/To/Your/File.txt", "w") as f: # Opens file and casts as f
f.write("Hello World form " + f.name) # Writing
# File closed automatically
There are many more methods but these two are most common. Hope this helped!
Hi,
I don't know if anyone has experience with this, but I want to create a script that writes python code, to a different python file when it is ran. I don't know if there is a name for this, or if it is even possible. Any pointers would be great, thanks
Open can be used in a several modes, in your case you have opened in read mode ('r'). To write to a file you use the write mode ('w').
So you can get a file object with:
open('1.txt', 'w')
If 1.txt doesn't exist it will create it. If it does exist it will truncate it.
You can use open() to create files. Example:
open("log.txt", "a")
This will create the file if it doesn't exist yet, and will append to it if the file already exists.
We use Jinja2 to fill in a template. It's much simpler.
The template looks a lot like Python code with a few {{something}} replacements in it.
This is pretty much the best way to generate Python source code. However, you can also generate Python executable code at runtime using the ast library. You can build code using the abstract syntax tree, then pass it to compile() to compile it into executable code. Then you can use eval() to run the code.
I'm not sure whether there is a convenient way to save the compiled code for use later though (ie. in a .pyc file).