Python - generate filename from supplied pattern - python

Before I get too far into making my own module, I wanted to ask if there is already something like this available. I'm making an application that creates output files from input files, and I would like the ability to name the output file based on a configurable template. For instance, the template might look like the following:
"Output_"<DATE:YYYYMMDD>"_"<DATE:HHMMSS>".txt"
to create files like Output_20190606_130612.txt
or it might look like:
<SEQUENCE:00-99>"-"<DATE:YYYYMMDD>".dat"
to create files like 13_20190606.dat
Is there any module in Python that already has this functionality?
EDIT:
The template will be user-configurable, e.g. in a configuration file that the main application reads. The template will be a combination of fixed strings and fields that will be populated by the application, but the application won't know ahead of time which fields will be used in the configuration.

You can simply create a template string like this
template = "Output_{YYYYMMDD}_{HHMMSS}.txt"
And then when saving the file format the string
file_name = template.format(YYYYMMDD="20190606", HHMMSS="130612")

Related

Accessing Windows File "Tags" metadata in Python

How do I access the tags attribute here in the Windows File Properties panel?
Are there any modules I can use? Most google searches yield properties related to media files, file access times, but not much related to metadata properties like Tags, Description etc.
the exif module was able to access a lot more properties than most of what I've been able to find, but still, it wasn't able to read the 'Tags' property.
The Description -> Tags property is what I want to read and write to a file.
There's an entire module dedicated to exactly what I wanted: IPTCInfo3.
import iptcinfo3, os, sys, random, string
# Random string gennerator
rnd = lambda length=3 : ''.join(random.choices(list(string.ascii_letters), k=length))
# Path to the file, open a IPTCInfo object
path = os.path.join(sys.path[0], 'DSC_7960.jpg')
info = iptcinfo3.IPTCInfo(path)
# Show the keywords
print(info['keywords'])
# Add a keyword and save
info['keywords'] = [rnd()]
info.save()
# Remove the weird ghost file created after saving
os.remove(path + '~')
I'm not particularly sure what the ghost file is or does, it looks to be an exact copy of the original file since the file size remains the same, but regardless, I remove it since it's completely useless to fulfilling the read/write purposes of metadata I need.
There have been some weird behaviours I've noticed while setting the keywords, like some get swallowed up into the file (the file size changed, I know they're there, but Windows doesn't acknowledge this), and only after manually deleting the keywords do they reappear suddenly. Very strange.

Batch Scripting-Python

I am new to python, I am creating an application which requires multiple batch files to be generated. There are only few minor differences in every batch files. Is there any way i can create a batch file using code?
Yes, you can
A Batch file is just a normal text file, which is executable. In order to get the File executable, you need to:
Set File permissions, (On Linux via chmod +x)
Tell the computer how to execute the file, this is done via the file ending on Windows and via the Shebang on Unix systems
Now, that we have coverd the basics, We can look at how to create the file.
since you say, you're files are very similar you should create a template for your file.
This can be done using a Docstring, or a separate file, it is important however, that you replace all positions in the file, where you want a custom value by {}.
Using this template you can use the format mini-language inorder to generate your file.
In python pseudo-code this will look like this:
template = """#/bin/bash/ \n My template with {} specification and {} specification"""
spec_1 = "example"
spec_2 = "present"
with open('my_batch_file', 'w') as file:
file.write(template.format(spec_1, spec_2))

How would I extract/update values in a file through a separate python script?

I am creating a simple account/password manager.
I will have pw.py run from the command line, and it will allow me to retrieve passwords, or add new ones from a file that exists in the same directory level like so:
|
|__ pw.py
|__ account_info.txt
Within pw.py, I want to import the data from account_info.txt to update, delete, or read.
Based on my reading, I believe the easiest way to do this would be to allow account_info.txt to be a JSON object, and to treat it as a dictionary from pw.py.
Is this possible?
Can I format the information in the .txt file in JSON, and from the py script,
with
open("account_info.txt") as account_file:
account_data = json.load(account_file)
and then input new information to update the text file by accessing the JSON dictionary like:
account_data[new_account] = new_password
Might this work? What are potential pitfalls I should look out for? Or what would be a better way to go about this?

How to permanently update a path variable in a python script

I'd like to know if there's a way to dynamically edit the lines in my scripts depending on a user input from an arcpy tool that I have created. the interface will allow the user to pick a file, then there's a boolean check to permanently change the default directory if the program was run later.
if you are familiar with arcpy:
def example():
defaultPath="C:\\database.dbf"
path=arcpy.GetParameterAsText(0)#returns a string of a directory
userCheck=arcpy.GetParameterAsText(1)#returns a string "TRUE"/"FALSE"
if path=="":
path=defaultPath
The goal here is to change "defaultPath" permanently if "userCheck" is "TRUE". I think it's possible to do that with the use of classes? or do I need to have an "index" table and refer to it's cells (like an excel sheet).
Thanks

Extract files from zip folder and store these files in blobstore

i want to upload zip folder from file input in form the i want to extract the contents of this uploaded zip folder,and store the contents (files)of this zip in the blobstore in order to download them after putting these files in one folder,but the problem is that i can't deal with the zip folder directly(to read it), i tried as this:
form = cgi.FieldStorage()
file_upload = form['file']
zip1=file_upload.filename
zipstream=StringIO.StringIO(zip1.read())
But the problem still that i can't read the zip as previous,also i tried to read zip folder directly like this:
z1=zipfile.ZipFile(zip1,"r")
But there was an error in this way.Please can any one help me.Thanks in advance.
Based on your comment, it sounds like you need to take a closer look at the cgi module documentation, which includes the following:
If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute...
This suggests that you need to modify your code to look something like:
form = cgi.FieldStorage()
file_upload = form['file']
z1 = zipfile.ZipFile(file_upload.file, 'r')
There are additional examples in the documentation.
You don't have to extract files from the zip in order to make them available for download - see this post for an example of serving direct from a zip. You can adapt that code if you want to extract the files and store them individually in the blobstore.

Categories

Resources