Taking txt files with unknown names as parameters (Python) - sys

I am trying to write a program in Pycharm that can take the name of 2 txt files as an input and then use that input to execute some set of instructions. I understand I can put the name of a txt file in the parameters of the configuration, but how can I do that without knowing what the file name will be beforehand? The code snippet is very vague so far but I can't really do test runs without passing the 2 txt files.
import sys
sets = sys.argv[1]
operations = sys.argv[2]
print(len(sys.argv))
print(sets)
print(operations)
Even when I've tried hard-coding names of txt-files as parameters in the configuration I only get back the names of the files so I know I have more problems than one 😬.

"I understand I can put the name of a txt file in the parameters of
the configuration, but how can I do that without knowing what the file
name will be beforehand."
In order to work with parameters unknown at the time you write your code, a filename for example in your case, you can pass those variables when you execute your python file in the terminal as arguments.
"Even when I've tried hard-coding names of txt-files as parameters in
the configuration I only get back the names of the files"
If you want to work with the text inside the files you need to open the files first, you can't just print them like print("file1.txt")
Code example:
import sys
file1 = sys.argv[1]
file2 = sys.argv[2]
print(f"Open and read file: {file1}")
with open(file1, "r") as f:
print(f.read())
print(f"Open and read file: {file2}")
with open(file2, "r") as f:
print(f.read())
Executing code (run in terminal):
python test.py test1.txt test2.txt
Output:
Open and read file: test1.txt
Hello World
This is file test1
Open and read file: test2.txt
Hello Stackoverflow
This is test2

Related

Save as a .txt file CMD/powershell outputs

I write a command like tree and all outputs printed on the console.
I wanna save the printed outputs as a .txt file, the file path is gonna set the target terminal path.
How can I do that?
This line does what you are asking for. It should work both in CMD and PowerShell.
tree >> myfile.txt

LibreOffice Command Line Convert CSV to DBF

I am working on a workflow where I need to be able to batch convert CSV files into DBF files. The data in the CSV files can all be output as TEXT fields and then saved as a DBF. I have tried to get any output possible using the command line in both Mac OS and Windows 7. While I was a little more successful in Windows, I was not able to get any conversion.
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to dbf --
infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2
/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2
/68/2/69/2/70/2/71/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\*.csv --outdir C:\Users\jdavidson\desktop\complete
So, I needed to have an --infilter on the command line to be able to do the conversion, but now it will only process 1 file and not batch file when using *.csv So, my next hurdle is to get over that.
The below helps to explain --infilters in more detail:
https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options#Filter_Options_for_Lotus.2C_dBase_and_DIF_Filters
Any easier way to find the filter sequence you need is by following the below listed steps:
Open the CSV Manually using LibreOffice and Specify Every Detail in the import Wizard
Run the below macro and it will show you the filter options in an input box, so you can copy the string in to your own code
Sub showFilterOptions
Dim args(),i%
args() = thisComponent.getArgs
for i = 0 to uBound(Args())
if args(i).Name = "FilterOptions" then inputbox "","",args(i).value
next
End Sub
Credit to code: https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=14018
The parameters need to be reversed. From https://ask.libreoffice.org/en/question/2641/convert-to-command-line-parameter/:
soffice --headless --convert-to <TargetFileExtension>:<NameOfFilter> file_to_convert.xxx
Using the old windows command prompt:
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --headless --convert-to dbf C:\Users\jdavidson\Desktop\DBFCONVERT\test.csv
This will create test.dbf in the current directory, which is perhaps C:\Users\jdavidson or, if running as administrator, C:\Windows\system32. To print the working directory, enter pwd, and then dir to see the file.
In powershell, add "& " in front to execute LibreOffice. Then enter gci or ls to see the file.
Be sure that a LibreOffice process is not already running in the background.
I was able to get this to work using the below code:
"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete
Please note, that I needed to use DBF and include an --infilter parameter field
There are two websites in my original posting that shows helpful scripts and infilter information to help other users

Merge txt file: TXTcollector

I am looking for an easy way (without windows commands) to merge several txt files.
Does anyone has an advice about the software TXTCollector? Is it a good one?
Thanks
A command prompt or .BAT/.CMD file can accomplish this easily by redirecting the output to a file.
type first.txt > merge.txt
type second.txt >> merge.txt
type third.txt >> merge.txt
A single redirection operator (e.g. >) will always create a new file with the output, overwriting any existing target file. Double redirection operators (e.g. >>) will append the output to an existing file or create a new file if one does not exist.
In the above .CMD script, the contents of first.txt, second.txt and third.txt will be in the new merge.txt.
EDIT Sample .CMD files
In its simplest form, a .CMD script that loops through the .txt files in a folder would look something like this.
#echo OFF
FOR %%c in (*.txt) DO TYPE %%c >> merge.txt
There is plenty of examples available on the web if you need to make adjustments for your particular situation.

Run commandline on multiple files in folder

I'd like to have a simple vbscript or even just a commandline one-liner that allows me to (in pseudocode):
for each file FL (*.ts) in folder
run command 'ffprobe -show_frames -select_streams 1 -print_format csv -i "folder\FL" > "folder\FL.Name.csv"'
next
Like the code says, I only want it to work on files of type .TS, and I need to run each file in the folder through that commandline and redirect the stdout to a csv file. I could create a small program in C#, but I'd rather have a lightweight script or something and I'm new to that. Any ideas?
Processing files with a particular extension in a given folder can be done like this:
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\foo").Files
If fso.GetExtensionName(f) = "ts" Then
sh.Run "...", 0, True
End If
Next
Redirecting the output to a CSV file is (IMHO) best done by running the VBScript with cscript.exe and redirecting STDOUT to a file:
cscript your.vbs > output.csv

How to handle this situatiuon in Perl

I am having a configuration INI file to store all configuration required for my script to run. I have a Logger.PM which uses Log4Perl, and ConfigReader.PM which reads the INI file and stores the value in global variable. My Start.PL is the entry point where i call the methods from Logger and configreader.
What I do currently
In Start.PL I hardcoded the INI file path
In Logger.Pm I harcoded the directory name where log files should be stored
What I want
I want the INI file path as configurable
I want the log folder path to be taken from the INI file
I could do this by following
Pass the INI file path as a parameter to the start.pl
Read the INI file and get the folder path from INI file
What I could face is that
I cannot use the Logger.PM in ConfigReader (partially) since the
folder name required for logger is part of INI file
I want to log every step of my script (for logging/debugging purpose in case of failure. )
I can use print but this will write to console and to capture i need to use >>log.txt. Then i will be forced to maintain 2 logs for my application which is not what I wanted
Anyone have a good solution for this scenario?
You can pass INI file path in command line using Getopt::Long, and command line switches for istance:
Start.pl --ini=/path/to/INI_file
Here is a code sample to show what changes are needed in Start.pl, in order to have switches:
#!/usr/bin/env perl
use v5.12;
use strict;
use Getopt::Long;
# That little tiny 's' after 'ini=' is for string
GetOptions ( 'ini=s' => \my $ini_file );
say $ini_file;
After this change, you can read all options from your INI file, including log folder path ( are you already using a module to manage INI files like Config::IniFiles? ).
There is something still unclear in your question about print: although one of my master said that print with a pair of square brackets is the best debugger in the world, why use print when you have set up Log::Log4perl?
When you say that Logger.PL can't be used in ConfigReader, are you referring to the log object?