I have a problem with opening the .bashrc file in python. I have written the following code:
mydata = open ('/home/lpp/.bashrc', 'r')
and I get this:
$ /usr/bin/python2.7 /home/lpp/lpp2016/Handin3.py
Process finished with exit code 0
Python does not open the file .bashrc. What did I do wrong?
This is an old question. I went through the same problem and this is my solution to read the content.
with open('.bashrc', 'r') as content:
fileContent = content.read()
print(fileContent)
fp = open(os.path.join((os.path.expanduser('~'), '.bashrc'))
fp.read()
it is opening and reading all the content
Related
Here I transfer a shell script from my sytem to raspberry pi using ssh and run the script on Rpi
python3 paswd.py | scp replace.py pi#192.168.1.$i:
python3 paswd.py |ssh -tt pi#192.168.1.$i 'chmod u+x ~/'replace.py' && exit'
This happens and it shows no errors
Replace.py
def replace():
fout=open("launch.sh","rt")
for line in fout:
fout.close
fout=open("launch.sh","wt")
fout.write(line.replace('chromium-browser','chromium-browser --kiosk '))
fout.close
replace()
launch.sh
echo "connected, launching browser"
chromium-browser & disown
After the replace.py is executed am not geting the require results, I get an empty launch.sh or no changes at all. As said it shows no errors. Please let me know where is my mistake. The saame code worked perfectly for .txt file, it replaces "chromium-browser" with "chromium-browser --kiosk" but on .sh file am not getting it.
Thanks for the help
This will solve your problem.
def replace():
file_path = "launch.sh"
find = "chromium-browser"
replacement = "chromium-browser --kiosk"
with open(file_path) as f:
s = f.read()
s = s.replace(find, replacement)
with open(file_path, "w") as f:
f.write(s)
replace()
this is my code to open a file and get the text from it :
f = open("C:/Users/muthaharsh/Documents/Harsh/News
Project/Part3/Testing_purposes/Downloads3/Are-you-being-churned-,-
Mint.txt","r+")
text = f.readlines()
print(text)
but i keep getting the error :
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/muthaharsh/Documents/Harsh/News Project/Part3/Testing_purposes/Downloads3/Are-you-being-churned-,-Mint.txt'
What code do i write to be able to do this ?
Thanks in advance ...
It's the whitespace in the path to file. Either use
r"C:/Users/muthaharsh/Documents/Harsh/News Project/Part3/Testing_purposes/Downloads3/Are-you-being-churned-,-Mint.txt"
or remove the whitespace in the filepath.
If you are running the code on windows add r before your filepath string.
Another way is that you can provide your input file as system arguments.
import sys
file_name = sys.argv[1]
with open(file_name, 'r') as f1:
file_text = f1.read()
print(file_text)
eg: python program for reading the file
you can run the code considering script is saved as readFile.py:
D:\Programs>python readFile.py D:\test.txt
output: This is a sample file
Here is the text
Hi Guys i'm a beginner in python,
The problem is this...
So when i open again the file, i try to add a newline at the end of file.
this is a piece of code:
url = "https://www.amazon.it/seven-deadly-sins-15/dp/8822602994"
command = "curl -i "+url
print command
file = subprocess.check_output(command,shell=True)
soup = BeautifulSoup(file,"html.parser")
out_file = open("nomi.txt","wb+")
s = soup.find('span',id='productTitle').get_text()+","
dim = sys.getsizeof(s)
out_file.seek(0,2)
print s
print dim
out_file.write(s)
out_file.close()
But it doesn't work.
Thanks for their help.
Have a nice Day.
You'll want to open the file in binary "b" or "rb" mode. You can then use seek to move to a given position. It would also be a good idea to use a context manager so you don't have to worry about closing the file when you're done. For example -
with open("file.txt", "rb") as f:
f.seek(2)
# do some other stuff with f
I try to use urllib to crawl this file: http://www.anzhi.com/dl_app.php?s=68611, but always download a wrong file(size smaller). However, if I open up this link on chrome, it goes well and the downloaded file size is correct. The code is attached, what's the problem?
import urllib
apk = "http://sc.hiapk.com/Download.aspx?aid=294091"
local=r'x.apk'
webFile = urllib.urlopen(apk)
localFile = open(local, "w")
realurl = webFile.geturl()
print realurl
realFile = urllib.urlopen(realurl)
localFile.write(realFile.read())
webFile.close()
realFile.close()
localFile.close()
What OS are you on? This line of code:
localFile = open(local, "w")
opens a text-mode file on Windows, which will do things that you don't want. Does changing that to
localFile = open(local, "wb")
(opening the file in binary mode) make things work correctly?
You're not using the same URL in your code that you're asking about in the question. Use the anzhi.com URL and you'll get the file you want. :)
I am trying to open a file in "w" mode with "open()" function in python.
The filename is : 仿宋人笔意.jpg.
The open function fails with this filename but succeeds with normal files.
How can I open a file with names which are not in English in python?
My code is as follows:
try:
filename = urllib.quote(filename.encode('utf-8'))
destination = open(filename, 'w')
yield("<br>Obtained the file reference")
except:
yield("<br>Error while opening the file")
I always get "Error while opening the file" for non-english filenames.
Thanks in advance.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import codecs
f=codecs.open(u'仿宋人笔意.txt','r','utf-8')
print f.read()
f.close()
worked just fine here
If you're having a problem it seems more likely to do with your operating system or terminal configuration than Python itself; it worked ok for me even without using the codecs module.
Here's a shell log of a test that opened an image file and copied it into a new file with the Chinese name you provided:
$ ls
create_file_with_chinese_name.py some_image.png
$ cat create_file_with_chinese_name.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
chinese_name_file = open(u'仿宋人笔意.png','wb')
image_data = open('some_image.png', 'rb').read()
chinese_name_file.write(image_data)
chinese_name_file.close()
$ python create_file_with_chinese_name.py
$ ls
create_file_with_chinese_name.py some_image.png 仿宋人笔意.png
$ diff some_image.png 仿宋人笔意.png
$
Worked for me, the images are the same.
If you yield your filename does it look right? I'm not sure if the filname has been mangled before reaching this code segment.
I tried modifying my code and rewrote it as:
destination = open(filename.encode("utf-8"), 'wb+')
try:
for chunk in f.chunks():
destination.write(chunk)
destination.close()
except os.error:
yield( "Error in Writing the File ",f.name)
And it solved my error.
Thank you everyone for taking time to answer. I haven't tried the options mentioned above as I was able to fix it, but thanks everybody.
This:
filename.encode('utf-8')
tries to convert filename from ascii encoding to utf-8 and causes the error.