Update /.aws/config file on your local machine - python

I am trying to update the local aws/config file on my mac. I am not able to read or update any contents of the config file.
import configparser
import os
creds_dir = os.path.dirname("~/.aws/config")
config = configparser.RawConfigParser()
if not config.has_section("some"):
config.add_section("some")
# METHOD -1
config.set("some", 'aws_access_key_id', "access_key")
config.set("some", 'aws_secret_access_key', "secret_key")
config.set("some", 'aws_session_token', "token")
config.set("some", 'aws_security_token', "token")
config.write("~/.aws/config")
# METHOD -2
with open('~/.aws/credentials', 'a') as file_out:
file_out.write("[profile]")
file_out.write("aws_access_key_id = aaa")
file_out.write("aws_secret_access_key = bbb")
file_out.write("aws_session_token = cccc")
I am getting an error:
FileNotFoundError: [Errno 2] No such file or directory: '~/.aws/credentials'
I could open the file from my mac terminal and view it.

Change this to be:
creds_file = os.path.expanduser("~/.aws/config")
or
with open(os.path.expanduser("~/.aws/config"), 'a') as file_out:
The ~ expansion is part of the shell normally and so you need to expand it. expanduser returns a string that has the full path name to the file.

Related

How to delete file in python inside docker container?

The code below is part of my code. In my local machine, everything is fine.However, I deploy my code and inside of docker container,it gives the error "result": "[Errno 13] Permission denied: path". What could be solution to delete in docker container also? I tried os.remove() also,It didn't work.
path = "/mypath/"
output = path + "myfile.pdf"
result_file = open(output, "w+b")
pisa_res = pisa.CreatePDF(
source_html,
dest = result_file)
result_file.close()
with open(output, "rb") as pdf_file:
encoded_string = base64.b64encode(pdf_file.read())
os.system(f"rm -rf {output}")
I don't know what is the problem with this file and how to delete it
but I would use io.BytesIO to create file in memory and then it doesn't create file on disk and it doesn't need to delete it
I don't have pisa to test it but it should be something like this
import io
result_file = io.BytesIO()
pisa_res = pisa.CreatePDF(
source_html,
dest=result_file)
result_file.seek(0) # move to the beginning of file to read it
encoded_string = base64.b64encode(result_file.read())
Module io is standard module so you don't have to install it.

Script can't save data to file

Based on *.blend file I have to write a script that gets informations about objects and saves them to json. This script can be opened in Blender, or running. The launch should save the json file with the data in the current directory.
So I created this:
import bpy
import json
objects = bpy.context.scene.objects
data = {}
for ob in objects:
item = {}
item['location'] = ob.location
if ob.name == 'Cube':
item['material_name'] = ob.active_material.name
data[ob.name] = item
elif ob.name == 'Camera':
item['camera_type'] = ob.data.type
data[ob.name] = item
elif ob.name == 'Lamp':
item['lamp_type'] = ob.data.type
data[ob.name] = item
with open('scene_objects.json', 'w') as json_file:
json.dump(data, json_file)
However, when I run the script in Blender, I received the following error:
PermissionError: [Errno 13] Permission denied: 'scene_objects.json'
I'm a beginner in using Blender so maybe it's impossible to write to file from Blender? However, if I can do it, I am asking for advice on how?
Your issue isn't with blender, the OS is preventing the creation (or writability) of the file based on file system permissions.
The line -
with open('scene_objects.json', 'w') as json_file:
will create a new file (or open existing) in the current working directory. When running blender that could be one of several options, depending on which OS you are using. It is also possible that starting blender from a GUI can leave you without a valid CWD, or a temporary dir that a user does not have permission to write to.
You can use os.chdir() to change the CWD to one that you know exists and that you can write to. You can also specify a full path instead of just a filename.

Python - Problem with writing file Errno 2

I am a Python newbie and I am having a problem that probably has as a simple answer. I have the following script, which works most of the way, I just get stuck trying the write the output file. The error I get is at the very end: IOError: [Errno 2] No such file or directory: '/D/1_NEW_ANALYSIS/Scripts/Melodic_fsfs/design_Rat01_Run_1.fsf'
Here is the code:
import os
import glob
studydir = 'D:/1_NEW_ANALYSIS'
fsfdir="%s/Scripts/Melodic_fsfs"%(studydir)
templatedir="%s/Scripts/Templates"%(studydir)
subdirs=glob.glob("%s/Subjects/Rat_[0-9][0-9]/Run_[0-2]"%(studydir))
for dir in list(subdirs):
splitdir = dir.split('\\')
# YOU WILL NEED TO EDIT THIS TO GRAB sub001
splitdir_sub = splitdir[1]
subnum=splitdir_sub[-2:]
splitdir_run = splitdir[2]
runnum=splitdir_run[-1:]
print(subnum)
replacements = {'SUBNUM':subnum, 'RUNNUM':runnum}
with open("%s/Melodic_design.fsf"%(templatedir)) as infile:
with open("%s/design_Rat%s_Run_%s.fsf"%(fsfdir, subnum, runnum), 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
Anybody have an idea why it doesn't work?
Thanks a lot!
If you are running on windows (I assume you are), studydir should look like:
studydir = 'D:\\1_NEW_ANALYSIS'

Python: Crossplatform code to download a valid .zip file

I have a requirement to download and unzip a file from a website. Here is the code I'm using:
#!/usr/bin/python
#geoipFolder = r'/my/folder/path/ ' #Mac/Linux folder path
geoipFolder = r'D:\my\folder\path\ ' #Windows folder path
geoipFolder = geoipFolder[:-1] #workaround for Windows escaping trailing quote
geoipName = 'GeoIPCountryWhois'
geoipURL = 'http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip'
import urllib2
response = urllib2.urlopen(geoipURL)
f = open('%s.zip' % (geoipFolder+geoipName),"w")
f.write(repr(response.read()))
f.close()
import zipfile
zip = zipfile.ZipFile(r'%s.zip' % (geoipFolder+geoipName))
zip.extractall(r'%s' % geoipFolder)
This code works on Mac and Linux boxes, but not on Windows. There, the .zip file is written, but the script throws this error:
zipfile.BadZipfile: File is not a zip file
I can't unzip the file using Windows Explorer either. It says that:
The compressed (zipped) folder is empty.
However the file on disk is 6MB large.
Thoughts on what I'm doing wrong on Windows?
Thanks
Your zipfile is corrupt on windows because you're opening the file in write/text mode (line-terminator conversion trashes binary data):
f = open('%s.zip' % (geoipFolder+geoipName),"w")
You have to open in write/binary mode like this:
f = open('%s.zip' % (geoipFolder+geoipName),"wb")
(will still work on Linux of course)
To sum it up, a more pythonic way of doing it, using a with block (and remove repr):
with open('{}{}.zip'.format(geoipFolder,geoipName),"wb") as f:
f.write(response.read())
EDIT: no need to write a file to disk, you can use io.BytesIO, since the ZipFile object accepts a file handle as first parameter.
import io
import zipfile
with open('{}{}.zip'.format(geoipFolder,geoipName),"wb") as f:
outbuf = io.BytesIO(f.read())
zip = zipfile.ZipFile(outbuf) # pass the fake-file handle: no disk write, no temp file
zip.extractall(r'%s' % geoipFolder)

Error when trying to read and write multiple files

I modified the code based on the comments from experts in this thread. Now the script reads and writes all the individual files. The script reiterates, highlight and write the output. The current issue is, after highlighting the last instance of the search item, the script removes all the remaining contents after the last search instance in the output of each file.
Here is the modified code:
import os
import sys
import re
source = raw_input("Enter the source files path:")
listfiles = os.listdir(source)
for f in listfiles:
filepath = source+'\\'+f
infile = open(filepath, 'r+')
source_content = infile.read()
color = ('red')
regex = re.compile(r"(\b be \b)|(\b by \b)|(\b user \b)|(\bmay\b)|(\bmight\b)|(\bwill\b)|(\b's\b)|(\bdon't\b)|(\bdoesn't\b)|(\bwon't\b)|(\bsupport\b)|(\bcan't\b)|(\bkill\b)|(\betc\b)|(\b NA \b)|(\bfollow\b)|(\bhang\b)|(\bbelow\b)", re.I)
i = 0; output = ""
for m in regex.finditer(source_content):
output += "".join([source_content[i:m.start()],
"<strong><span style='color:%s'>" % color[0:],
source_content[m.start():m.end()],
"</span></strong>"])
i = m.end()
outfile = open(filepath, 'w+')
outfile.seek(0)
outfile.write(output)
print "\nProcess Completed!\n"
infile.close()
outfile.close()
raw_input()
The error message tells you what the error is:
No such file or directory: 'sample1.html'
Make sure the file exists. Or do a try statement to give it a default behavior.
The reason why you get that error is because the python script doesn't have any knowledge about where the files are located that you want to open.
You have to provide the file path to open it as I have done below. I have simply concatenated the source file path+'\\'+filename and saved the result in a variable named as filepath. Now simply use this variable to open a file in open().
import os
import sys
source = raw_input("Enter the source files path:")
listfiles = os.listdir(source)
for f in listfiles:
filepath = source+'\\'+f # This is the file path
infile = open(filepath, 'r')
Also there are couple of other problems with your code, if you want to open the file for both reading and writing then you have to use r+ mode. More over in case of Windows if you open a file using r+ mode then you may have to use file.seek() before file.write() to avoid an other issue. You can read the reason for using the file.seek() here.

Categories

Resources