Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
@MichelleMorales how do I go about passing the filename to the script as an argument instead of hard-coding it?
@TheLetterM you could try something like this…
First, in your script add:
import sys
path = sys.argv[1]
Then, when you run it:
- python script.py file_name.txt
@MichelleMorales if I have a Django webapp running off DigitalOcean, can I use the Django webapp to save csv files onto the DigitalOcean droplet?
@michellemorales I have a question, in your below script, you use write() 2 times, the 2nd time will overwrite the 1st one, so you will get the same content as days.txt. Instead you should open the 2nd file with append mode, this way it will have title. Also, the last 2 print methods, only print the variable content, it have nothing to do with the new file content.
You should consider using the with keyword when reading or writing to a file. It will close the stream for you automatically.
Hi all, It does not work for me, I always seem to have the same error:
Traceback (most recent call last): File “text.py”, line 1, in <module> text.txt = open(‘/C/Users/louiscigrang/upython/text.txt’) FileNotFoundError: [Errno 2] No such file or directory: ‘/C/Users/louiscigrang/upython/text.txt’
This is the input in my .py file:
f = open(‘/C/Users/louiscigrang/upython/text.txt’, ‘r’) file_data = f.read() f.close()
print(file_data)
Both of my files are in the same directory in a folder called upython that’s in my directory (louiscigrang).
Can anybody help me please?
Kind regards,
Louis
Using the with open() as syntax is simpler, safer, and more Pythonic than manually calling .close()
I kept getting this error:
Traceback (most recent call last): File “C:/Program Files (x86)/Python37-32/testw.py”, line 5, in <module> dance = open(path,‘r’) OSError: [Errno 22] Invalid argument: ‘C:\Program Files (x86)\Python37-32\testforweb.txt’
so I changed my path to this:
path = ‘C:/Program Files (x86)/Python37-32/testforweb.txt’
but then this is what I got:d
<_io.TextIOWrapper name=‘C:/Program Files (x86)/Python37-32/testforweb.txt’ mode=‘r’ encoding=‘cp1252’>
This is my code:
path = ‘C:/Program Files (x86)/Python37-32/testforweb.txt’ dance = open(path,‘r’) moon = dance.read() print (dance) dance.close()
What I don’t understand is why am I getting invalid arguments or something else, when I copy and pasted the location of the text file?