Print all files in the sub-folders of a folder [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm planning on using Robobasket to organise my folders like this,
Invoices
- Jan/Feb/Mar
- Customer Number 1/Customer Number 2
- Then the dates the invoices actual arrived on
The files are named, Month/Customer Number/Date.
What I need to be able to do is, right click on the month, and have it print all the invoice for that month. Specifically printed out by Customer Number 1, then all the dates in order, say 3rd, 17th, 31st, then Customer Number 2, 12thst 22nd, etc.
I have several thousand invoices a month, so you can see why manually printing, even just by Customer number is not something I can do.
The system is running Windows 7, although any programs which can work with a server would be great too, as it's entirely possible, that'll happen soon.
I have basic script skills in C, Python, and Forthe; But am willing to learn for the sake of this issue.
The important thing is that someone who has very little computer skills can at the end of this set it going. Hence the desire for it to be accessible through the context menu.
Any and all help would be appreciated.

You have to do multiple things to get that working.
You need a Python environment installed on the machine or you have to pack your script as an executable. Use py2exe for this.
To get all the files in a given directory, you could use the glob module from python, if you need to filter. Or you could use the approach from denis.
You have to add a context menu item via the registry or using a context menu editor. Look there for an instruction.
Ideally, you pack everything into an installer to distribute the stuff. Including a batch script that adds the context menu item to the registry.

You can use this command to get all the files in current directory:
import os
files = os.listdir(os.curdir)
Now you have all the files in files variable. In order to print contents of all the files in current directory, you can follow this post
How do I print the content of a .txt file in Python?

Related

How can I pull data from Excel and transfer it to existing charts and tables on Powerpoint using Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Every week, I get sent the same excel files but with different values that are updated. What I usually do is manually pull these values from excel, and put them into the same charts and graphs in Powerpoint to update them. For example, if I get updated values for the 2nd week of July, I get these values, open PowerPoint, open the graph, and insert a new date with the corresponding values. This usually takes me a few hours a week to do. Is there any way I can run a script that would take the values of the excel file once I download them, and add them to my tables and graphs on my existing PowerPoint file? If so, how can I do it and what libraries can I use? Or is there a better way that I can automate this?
Yes, this is a fairly common use-case for python-pptx. You'll have to search separately for each of the finer points, but here's a general strategy I've seen work many times:
Use openpyxl to read the values you need from the Excel workbook (.xlsx file).
Use a "template" .pptx file that looks like the final product you want each week except the updatable values aren't up-to-date. This could be a special .pptx file you keep for this purpose or it could just be the one from last week. I recommend you start with the one from last week and then always use that same one as your template. That just keeps things more predictable. You would start by calling prs = Presentation("my-template.pptx") to begin the process.
Find the chart that needs updating. If you always use the same template that can just be chart = prs.slides[3].shapes[6].chart or whatever the right numbers are because it's always in the same place.
Create a new ChartData object containing the new numbers.
Call chart.replace_chart_data(new_chart_data) to replace the chart values.
Then prs.save(new_name_for_this_week) and you're done.
The python-pptx documentation is here: https://python-pptx.readthedocs.io/en/latest/index.html
I'd say work through the Getting Started exercise just as a warm-up and then to this example: https://python-pptx.readthedocs.io/en/latest/user/charts.html
After that, Google is your friend and most roads lead back here to StackOverflow where there are more that 500 questions with answers about specific aspects of python-pptx.

Processing large csv files in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have large CSV files containing more than 315 million rows and a single column. I have to process more than 50 such files at a time to get the results.
As I read more than 10 using a csv reader, it takes more than 12GB of RAM and is painfully slow. I can read only a chunk of the file to save memory but would spend more time in reading the file as it will read the whole file every time.
I have thought about loading them into a database and querying the data from there. However, I am not sure if this approach would help in any way. Can anyone please tell which is the most efficient way of handling such scenarios in Python?
You will find the solution here
Lazy Method for Reading Big File in Python?
Additionally, if you have a longer processing pipeline, you can look into Section 4.13. Creating Data Processing Pipelines in the book, Python Cookbook, 3rd edition by Beazly and Jones.
Check out ETLyte, a tool I've just open sourced. It's .NET, but you could call out to the EXE from Python. It's still a work in progress but I think it'll work for your situation.
With ETLyte, here would be the steps:
Place files in Flatfiles folder, or whichever folder you specify in the config.json.
Describe them with a JSON schema and place them in the Schemas folder, or whichever you specify (Note: If they all have the same schema [you said it's all a single column], then just change flatfile field in the schema to a regex that matches your files)
When it comes to performing the addition/multiplication, you could create derived columns that perform that calculation.
Run ETLyteExe.exe and allow the data to flow in
ETLyte is just getting started but it has a lot of features and a lot more on the roadmap. It also comes with an interactive REPL with word completion which wraps the SQLite DLL so you can interrogate the data without installing sqlite3. For an overview of the tool, look here.

How to call a function from every module in a directory in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What I want to do is import every module in a directory/package and then call the same function on each submodule, without knowing their names or how many there are.
If you're familiar with MCEdit, (a 3rd party tool for Minecraft) its filter system is like this. All the .py files which are filters go in a directory, and then MCEdit imports each one and is able to show a list of imported filters and execute each one's perform(level, box, options) function.
Find every module in that directory, use __import__ to import it, then call the function. For example:
for file_name in os.listdir('path/to/modules'):
if file_name.startswith('.') or not file_name.ends_with('.py'):
continue
module_name = file_name[:-3]
module = __import__(module_name)
module.some_function()
However, this does not account for all cases—in particular, some modules might be written in C and have the .pyd extension rather than .py. Do you want to account for that? There are probably several entries in sys.path. Do you want to support all of them, or only search one directory? Modules can reside inside of a ZIP file. You would have to support that. And with import hooks, modules could probably be generated on the fly, and there might be no way to enumerate them. You have to decide how far you want to go when searching for modules. But after you’ve found them, it’s easy to import and use them.

efficient database file trees [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So I was making a simple chat app with python. I want to store user specific data in a database, but I'm unfamiliar with efficiency. I want to store usernames, public rsa keys, missed messages, missed group messages, urls to profile pics etc.
There's a couple of things in there that would have to be grabbed pretty often, like missed messages and profile pics and a couple of hashes. So here's the question: what database style would be fastest while staying memory efficient? I want it to be able to handle around 10k users (like that's ever gonna happen).
heres some I thought of:
everything in one file (might be bad on memory, and takes time to load in, important, as I would need to load it in after every change.)
seperate files per user (Slower, but memory efficient)
seperate files
per data value
directory for each user, seperate files for each value.
thanks,and try to keep it objective so this isnt' instantly closed!
The only answer possible at this point is 'try it and see'.
I would start with MySQL (mostly because it's the 'lowest common denominator', freely available everywhere); it should do everything you need up to several thousand users, and if you get that far you should have a far better idea of what you need and where the bottlenecks are.

Phonebook python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi i am new to python an di recently created a phonebook using the dictionary function and then changed into exe using py2exe. I am facing a problem now i enter names in to the phone book and then when i exit the program and return back all the numbers are gone. SO is there any way to save the names and numbers entered into the program? Please give me the code as i am doing this for my class and they would be mad if the numbers disappeared everytime they exited the phonebook! PLEASE HELP!
If you do not yet want to learn relational databases, NoSQL or cloud solutions, you can start by using shelve module.
Well, the main problem is that you do store new values in your database, which is in this case represented with dict, but your don't save it's condition between scripts executions. The time of existing of an object in your script - while the the script is running by interpreter and an object has some links to it. When your restart your program - you're starting to run your script all over again, and it stores in the dict only the elements, which were specified during the script.
The most simple solution, in my opinion, is to use python pickle module. You're gonna save that dict in a file and then load it in the begging of your scrip and save it at the end.
you need to update script code with something like this:
default = {'Sarah': 7736815441,
'John': 7736815442}
def start():
#some code here, before you're trying to access phone numbers in your dict
try:
phonebook = pickle.load(open("data.pb", "r"))
except IOError:
phonebook = default
#your script here
def exit():
#some code here, last chance to modify your dict,
#so changes will appear in next program executions
pickle.dump(phonebook, open("data.pb", "w"))
hope you're familiar with python functions, if no - you can read about then here

Categories

Resources