Using app.open_resource('foobar.txt', 'w') generates the error Resources can only be opened for reading in flask.
Is there a way to open a resource to write to it?
If not, can you get the path of the resource using flask and then I can open it manually and write to it.
This should work:
import os
f = open(os.path.join(app.root_path, 'foobar.txt'), 'w')
This is more convenient:
import os
with open(os.path.join(app.root_path, 'foobar.txt'), 'w') as f:
...
Related
I have been struggling with this problem for a while but can't seem to find a solution for it. The situation is that I need to open a file in browser and after the user closes the file the file is removed from their machine. All I have is the binary data for that file. If it matters, the binary data comes from Google Storage using the download_as_string method.
After doing some research I found that the tempfile module would suit my needs, but I can't get the tempfile to open in browser because the file only exists in memory and not on the disk. Any suggestions on how to solve this?
This is my code so far:
import tempfile
import webbrowser
# grabbing binary data earlier on
temp = tempfile.NamedTemporaryFile()
temp.name = "example.pdf"
temp.write(binary_data_obj)
temp.close()
webbrowser.open('file://' + os.path.realpath(temp.name))
When this is run, my computer gives me an error that says that the file cannot be opened since it is empty. I am on a Mac and am using Chrome if that is relevant.
You could try using a temporary directory instead:
import os
import tempfile
import webbrowser
# I used an existing pdf I had laying around as sample data
with open('c.pdf', 'rb') as fh:
data = fh.read()
# Gives a temporary directory you have write permissions to.
# The directory and files within will be deleted when the with context exits.
with tempfile.TemporaryDirectory() as temp_dir:
temp_file_path = os.path.join(temp_dir, 'example.pdf')
# write a normal file within the temp directory
with open(temp_file_path, 'wb+') as fh:
fh.write(data)
webbrowser.open('file://' + temp_file_path)
This worked for me on Mac OS.
How do I access a CSV file on my computer in Jupyter notebook using os module?
I've tried the below code:
import os
file =
(r"C:\Users\...", "r")
text = file.read()
file.close
You can use it directly without os module:
with open(r"C:\Users\xyz\Desktop\test.csv","r") as f1:
lines = f1.readlines()
OR
f = open(r"C:\Users\xyz\Desktop\test.csv", "r")
You don't need to manually open and close the file, the keyword 'with' does that for you:
import csv
file = r'path\to\your.csv'
with open(file, 'r') as csvfile:
# do something
For more info please check the official documentation of the CSV module.
I tried this code posted 2 years ago:
import subprocess
with open("output.txt", "wb") as f:
subprocess.check_call(["python", "file.py"], stdout=f)
import sys
import os.path
orig = sys.stdout
with open(os.path.join("dir", "output.txt"), "wb") as f:
sys.stdout = f
try:
execfile("file.py", {})
finally:
sys.stdout = orig
It hangs up the terminal until I ctl-z and then it crashes the terminal but prints the output.
I'm new to coding and am not sure how to resolve. I'm obviously doing something wrong. Thanks for your help.
You can simply open and write to the file with write.
with open('output.txt', 'w') as f:
f.write('output text') # You can use a variable from other data you collect instead if you would like
Since you are new to coding, i'll just let you know that opening a file using with will actually close it automatically after the indented code is ran. Good luck with your project!
when i run this code in Python 3.4.2(win7-64) it doesn't work! it creates file but nothing in it.(0 bytes)
I don't know what is the problem? Help- Thanks
Windo
import pickle
f=open ("G:\\database.txt","wb")
pickle.dump (12345,f)
You have to close the file object that you have opened. So just add the line
f.close()
at the end and it will work.
As an alternative, you can also use the with statement to open the file, then it will automatically close the file for you when it's done:
import pickle
with open("G:\\database.txt", "wb") as f:
pickle.dump( 12345, f )
I wrote this web crawler program that accesses a website then it writes the output to an HTML file.
I have a problem with the following though. I am not able to open the output file with the web browser. However I can open URL's with the webbrowser module. Is it possible to open files using this method? If yes, how exactly can I do it?
import urllib
import webbrowser
f = open('/Users/kyle/Desktop/html_test.html', 'w')
u=urllib.urlopen('http://www.ebay.com')
f.write(u.read())
f.close()
webbrowser.open_new('/Users/kyle/Desktop/html_test.html')
If you are using python3, you should use urllib.request:
from urllib import request
filename = '/Users/kyle/Desktop/html_test.html'
u = request.urlopen('http://www.ebay.com')
with open(filename, 'wb') as f: #notice the 'b' here
f.write(u.read())
import webbrowser
webbrowser.open_new(filename)