Opening Test File on Cisco IOSXE python guestshell - python

I successfully transferred a text file (zero_conf.txt) to a Cisco IOSXE (Amsterdam) switch flash file system using python on the guestshell.
However when I try to open the text file using the following code:
with open('/flash/zero_conf.txt','r+') as f:
lines = f.readlines()
I am getting the following error:
with open('/flash/zero_conf.txt','r+') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/flash/zero_conf.txt'
Please let me know if I am making a syntax error or what will be the correct way to open a text file on the Cisco iosxe python guestshell.

Related

filenotfound when writing to a new file

I got this error message in Scrapy spider, when I was writing the html of website to file.
with open('{}.html'.format(name), 'wb') as html_file:
FileNotFoundError: [Errno 2] No such file or directory:'CHARCOAL.html'
98% of the websites html were written correctly, however some threw this error (different file names).To make sure I printed the names and they all exist.
this is the code I use to write to files
#code here
name = response.xpath('//*[#id="itemTitle"]//text()').extract()[1]
with open('{}.html'.format(name), 'wb') as html_file:
html_file.write(response.body)

Access html file in write mode using python

example.py
file = open("innovative.html", "w")
file.write("This is a test\n")
file.write("And here is another line\n")
file.close()
return render_template('innovative.html')
while I am executing this file in server it throwing [Errno 13] Permission denied: 'innovative.html'
I am executing this file in rhcloud.
I changed permissions for file as -rwx- for innovative.html
Thanks in advance.
This error can occur when a file is actively opened or utilized by another process at the same time (as well as when permissions are not given). Be sure to check that the file is not being used while attempting to write.

Python - IOError: [Errno 2] No such file or directory: u'lastid.py' for file in same directory. Works locally, doesn't on Heroku

I suspect this is a very newbie question but I can't find any solutions that are helping :(
I've been trying to get started with Python by building a simple Twitter bot which replies to people who tweet at it. It worked locally, and it doesn't work on Heroku.
A quick rundown: Each time the bot tweets, it uses a script called mainscript.py which writes the ID of the last tweet replied to into a separate file called lastid.py. The next time the script runs, it opens lastid.py, checks the number inside against the current list of tweets, and only responds to those with a larger ID number than the one stored in lastid.py.
fp = open("lastid.py", 'r')
last_id_replied = fp.read()
fp.close()
#(snipped - the bot selects the tweet and sends it here...)
fp = open("lastid.py", 'w')
fp.write(str(status.id))
fp.close()
This works great locally. Runs fine. However, when I upload it to Heroku I get this error:
Traceback (most recent call last):
File "/app/workspace/mainscript.py", line 60, in <module>
fp = open("lastid.py", 'r')
IOError: [Errno 2] No such file or directory: u'lastid.py'
I am absolutely 100% positive lastid.py and mainscript.py are on the server and inside the same directory - I have triple-checked this by running bash on heroku. My .gitignore file is blank so it isn't anything to do with that.
I don't understand why such a simple command as 'open a file in the same directory and read it' doesn't work on the server. What on earth have I done wrong?
(I realise I should have worked through some tutorials before trying to build something custom in a new language, but now I've started this I'd really love to finish it - any help anyone can offer would be very much appreciated.)
Probably the python interpreter is being executed from a different directory than where your script lives.
Here's the same setup:
oliver#aldebaran /tmp/junk $ cat test.txt
a
b
c
baseoliver#aldebaran /tmp/junk $ cat sto.py
with open('test.txt', 'r') as f:
for line in f:
print(line)
baseoliver#aldebaran /tmp/junk $ python sto.py
a
b
c
baseoliver#aldebaran /tmp/junk $ cd ..
baseoliver#aldebaran /tmp $ python ./junk/sto.py
Traceback (most recent call last):
File "./junk/sto.py", line 1, in <module>
with open('test.txt', 'r') as f:
IOError: [Errno 2] No such file or directory: 'test.txt'
To solve this, import os and use absolute pathnames:
import os
MYDIR = os.path.dirname(__file__)
with open(os.path.join(MYDIR, 'test.txt')) as f:
pass
# and so on

Function that takes file as input

I want to define a function that takes a file name, and runs the file through some code. I have completed the latter part, but I'm stuck at the first part. Here is where I'm having trouble:
def function(inputfilename):
file = open("inputfilename","r")
example input and the error I get:
>>>function("file.csv")
FileNotFoundError: [Errno 2] No such file or directory: 'inputfilename'
How can I fix this?
You are trying to open a file with inputfilename name.
Replace:
file = open("inputfilename", "r")
with:
file = open(inputfilename, "r")
Also, consider using with context manager while working with files.

How to read log files in python in django

I am trying to read the log files or any other file and display in web browser using django and python
I used this code
data_file = open('/var/log/samba', 'r') # note -- 'r' not r
data = data_file.readlines() # returns a list of lines
I get this error
[Errno 21] Is a directory: '/var/log/samba'
You're trying to open a directory. Don't do that. Open a file instead.

Categories