Filename Manipulation in Python - python-2.7

This should be a simple question with a simple solution but I searched online for hours and found nothing but very complicated answers.
I have a series of file as follows: blablabla_X.bla Where X is an integer going from 1 to a few hundreds. I want to open each file one at the time, do a little processing, close it and go on to the next one. Is there any simple ways to do this? I am thinking to separate the path, the body and then the extension of the file name, change the name and then put everything together.
Any suggestions will be greatly appreciated.
Cheers

Related

Searching .csv file and returning all lines with a specific string c++

Sorry this is my first post on here. I am very new to c++. I am currently working on a small project which requires reading data from a .csv file, searching for specific words and then writing all the lines that contain the specific word to they're own .csv file. I will be honest and say I am out of my depth when it comes to classes etc which is why I am asking you good people on here if you would have or know of any examples of this type of problem for me to try to understand it.
Thanks for anything at all!
Jay

How to redirect to the top of a .csv file using SetFilePointer

Basically, I'm creating an error log for a series of test to be ran that records failures and successes. The error log is a .csv file. Basically, the program is taking a list of tests and they are named #testlist.txt for example. Within each #testlist.txt is a series of tests.
For example,
#testlist.txt contains:
Test1.txt
Test2.txt
Test3.txt
Within Test1.txt, there are 3 subtests. These subtests can fail or succeed; hence, the success and failure rate. So what this error log reports is the statistics for each Test, and also a global success and failure rate for all the tests with the #testlist.
My assignment is to have the statistics of the #testlist at the top of the list instead of at the bottom. The problem is: I don't know how to redirect to the top of the file after all the individual test statistics have been printed to print the global statistics to the .csv file.
I am using C++ in Visual Basic 2008. Any suggestions would be extremely helpful! Thank you so much!
Update:
So this may seem like a dumb question, but I went to MSDN to look up SetFilePointer which is what my colleague suggested I try, but it wasn't making sense as to why it wasn't working in my code.
if (SetFilePointer(m_pDlg->sumLog,0,NULL,FILE_BEGIN) != INVALID_SET_FILE_POINTER){
fprintf(m_pDlg->sumLog, "\n\n%s,%s,%d,%d,%d,%0.2f%%,%0.2f%%", pDlg->testlistFileName, s, m_pDlg->m_uiValTotal,
m_pDlg->m_uiValPassed, m_pDlg->m_uiValFailedGlobal,
m_pDlg->getSuccessRate(m_pDlg->m_uiValTotal, m_pDlg->m_uiValPassed),
m_pDlg->getFailureRate(m_pDlg->m_uiValTotal, m_pDlg->m_uiValFailedGlobal));
}
Perhaps I'm understanding this method wrong and if anyone could shed some light on this and help me to get this working, that'd be greatly appreciated!
The problem: SetFilePointer is always equal to INVALID_SET_FILE_POINTER.
Some more information:
m_pDlg->sumLog
This is the name of the file I'm writing to. At the end of this file (maybe about 1GB of data), I'm trying to get to the beginning to insert this line.
I've declared it as
FILE* sumLog;
If more information is needed, I will be happy to provide.
Again, thank you so much!
Are you asking how to write new data at the top of the file after the file already has content?
If so, the answer is that trying to insert data anywhere but the end of the file is horribly inefficient. Basically, you'd need to shift every byte after the insertion point to a lower position in the file. This involves reading and rewriting each byte in a very complex way.
So the best solution is to write your new content to a new file, and then copy the original file to the new file, after your new content. That is much more efficient and easier to implement.
You cannot simply insert such variable length data to the start of an already opened file. You'll need a different approach:
You read in the .csv skipping the statistics line, and move it to a backup file.
You calculate the new statistics based on what was read in in step 1.
You reopen the .csv file as output print the header and statistics first, and afterwards print all of the other data lines that were used to calculate the statistics

Legends on graphs

I am new to using C++, and I have been trying to plot some basic graphs that include legends in them. I have been googling to find ways to do this, and found that chplot.h can do this, like in the link below:
http://www.softintegration.com/docs/ch/plot/
I want to download this header file online, but can't seem to find it. Is there a website that can do this? Or is there another header file that allows you to plot a legend on a graph?
Thanks very much!
A header file itself will most likely not allow you to do anything. You'd need the whole library (Ch professional in this case) for that. Have you read through the site you link to?
Anyway, they say they're based on GNUplot, so you might want to investigate that.
On the other hand, if you want to learn C++ rather than just use it for one particular task, there are easier and better suited problems to start with.

Concerning Converting Text file to Binary File c++

I've been at it all night just trying to get a simple program to read in a text file and then copy it/write it back into a binary format.
My code looped through the text doc, got the data, put it in the buffer and wrote it back out. Heck I even hard coded the data I wanted to be written out in binary.
I used fstream, ofstream, example: fp1.open("student.dat",ios::binary);
and was reading up on several different sites such as:
http://www.functionx.com/cpp/articles/serialization.htm
http://www.cppforschool.com/tutorial/files2.html
and I had working code, but when I open the .bin file in my Notepad++ I saw that my text data still looked like text and wasn't really 'converted' over to any hexdecimal format, or anything really. Numbers were, and I double checked to see if they were accurate by y'know, a little website where you can type in the number and it spits out the hex.
I was so fed up as to why my text wasn't converting that I destroyed all my code and tried to start over. *hence the lack of examples"
So, my question, finally is, why wasn't the text changing in any way, is this normal for a binary file and using this method? I've even used pre-made coding examples and it all came out the same way. Am I just expecting it to all look like 1's and 0's and really it's not and it was all really working?
My main project is to convert an .OBJ file to binary data, but really how should I be looking at this? How should this binary file look?
Any advice would be greatly appreciated!!!
Thank you!
I was just using Chars and string and wasn't seeing a difference. Once I started using other data types, it became apparent that there was a change. I understand that .OBJ and .txt are binary file types, i just expected a bigger change. I used cplusplus.com and reviewed what I needed to know more of. Thank you for trying to help I guess!

Moving, renaming huge amount of text files based on content and size

*Update July 4*
I ended up doing the following:
Sort on date
Check if last sentence is the same
If Yes: If bigger -> this is the new message to be chosen. If smaller: remove. If no more of the same can be found, choose this one and move to another folder.
If No: move on. Loop this again until all files with certain date have been checked.
Thanks all for the help!!
I'm busy with a big project where I have a huge number of emails that I have to filter, imported from gmail through thunderbird. There is a big problem though.
Because gmail uses conversations, but thunderbird doesn't format them as such, what I have is a text file for each email, though the complete previous conversation as well. And so a whole new text file for each reply.To clarify, an example of a conversation:
Me:Hi, how are you?
You, replying: Good!
Me: Great!
In gmail this looks exactly as above, but for me this are now 3 files:
file 1:
Me, sent at 11:41:
Hi, how are you?
file 2:
You, sent at 11:42:
Good!
Me, sent at 11:41:
Hi how are you?
file 3:
Me, sent at 11:43:
Great!
You, sent at 11:42:
Good!
Me, sent at 11:41:
Hi how are you?
As you can understand, this is no problem with 3 files: I just throw away file 1 and 2 and only use file 3. That's precisely what I want to do. But considering in total there are around 30k files, I would very much like to automate that.
It is unfortunately not possible to do this complete by file name, though partially it can. The files are named after their date. For instance: 20110102 for Jan 2, 2011. However as there are multiple email conversations on a day, I would lose a lot if I would just sort by date and only keep the largest.
I hope the problem is clear and you can help me with this.
I work on Mac OSX 10.7. I've tried using Applescript, but either my script is not good or Applescript can't handle the amount of files.
Maybe you have a recommendation for software or a script in some way? I'm open for all and not unfamiliar with programming.
Thanks in advance!
As your task is basically just text processing, any language you're familiar with, including AppleScript, PHP, bash, C, should be able to do the job. I think perhaps #inTide's breaking the problem down into discreet steps is what you need to do, building one portion at a time in the language of your choice.
Pick a language that you're familiar with and start writing one the code to the first step and make sure it's working as you expect, and then expand, adding a small bit of new functionality at each point and making sure that functionality works before moving on. Without an example of the code you've written or a better description of how AppleScript is failing for you, additional advice is difficult.