os.walk() is not throwing any error nor the result in python - python-3.7

I am trying to print the file names which are in a predefined paths where the paths are stored in paths.txt. But when I execute the below code, I'm not getting any error nor the files names printed.
import os
with open('D:\paths.txt', 'r') as file:
data = file.read()
path = data.split(";")
print(path)
for line in path:
for root, dirs, files in os.walk(line):
for name in files:
print(name)

You need to remove the double-quotes from the file (""). Here is why; When the file gets read by Python, after it does the .split(), the double-quote characters are part of the Python string. So instead of passing into os.walk() the path D:\bp1, you were actually passing in "D:\bp1", and there was no path that starts with a " that's why nothing was happening.
You would only need to provide the double quotes if you're writing the name in a terminal/command prompt and don't want to escape the double quotes, or if you're trying to define the string inside Python using the double quote literal, for example path = "D:\\bp1" (notice that in that case you also have to escape the \ with another one.

Related

Output filename to VSCode replace string

Say I have a search I want to do across my entire project: callToMyFunction()
And I would like to replace it with:
callToMyFunction();
console.log("calling function from ${filename}");
Is there a way (or an extension) that could do the ${filename} (or whatever) where the filename of the file where the replacement string is being applied gets written in?
If a search match was found in the file index.js the replacement would be:
callToMyFunction();
console.log("calling function from index.js");
[BONUS if there is a way to also get line number, but I'd be more than satisfied with just the filename 😁]

Passing file path with spaces and forward slashes as argument to TCL command

I have a TCL script that is run by Libero using a file path provided as an argument to open a project. The file path is C:\Users\me\Documents\FPGA projects\file.prjx
I am running the script according to Libero TCL Reference Guide (pages 51 - 52) to run the script on the command line. On page 47, the doc outlines how to work with filenames with spaces; using braces or in the case where it is used as an argument use double quotes.
The command I am trying to execute is:
Path\to\libero SCRIPT:export.tcl SCRIPT_ARGS:""C:\Users\me\Documents\FPGA projects\file.prjx""
The outer set of double quotes is to follow the syntax outlined in page 52 of the document for providing arguments and the inner set of double quotes is to handle the white space in the first argument. I had expected $argv 0 to be C:\Users\me\Documents\FPGA projects\file.prjx, but instead $argv 0 is actually C:\Users\me\Documents\FPGA.
I added a print statement to the script to print $argv:
puts $argv
This gives a result of C:\Users\me\Documents\FPGA so the rest of the file path is not being interpreted as even being a second argument.
My assumption is that the conventions outlined in the document are just standard TCL conventions for providing a file path containing forward slashes and spaces as an argument. I have not been able to find an example of passing a similarly formatted argument in TCL. Any ideas?
I'm not sure it's the same issue but, having MagicSplat tclsh.exe and wish.exe linked to *.tcl and *.tk files in Windows (7 to 11), calling scripts this way:
processDIR.tcl "c:\My directory\subdir"
evals arguments only till first space, becoming "c:\My" BUT calling scripts like this:
tclsh.exe processDIR.tcl "c:\My directory\subdir"
trespasses complete argument.
Could you give it a try?

Vertica COPY + FLEX table

I want to load on a flex table a log in which each record is composed by some fields + a JSON, the format is the following:
"concorde-fe";"DETAILS.SHOWN";"1bcilyejs6d4w";"2017-01-31T00:00:04.801Z";"2017-01-31T00:00:04.714Z";"{"requestedFrom":"BUTTON","tripId":{"request":3003926837969,"mac":"v01162450701"}}"
and (after many tries) I'm using the COPY command with a CSV parser in this way:
COPY schema.flex_table from local 'C:\temp/test.log' parser fcsvparser(delimiter=';',header=false, trim=true, type='traditional')
in this way all is loaded correctly except the JSON, that is skipped and left empty.
Is there a way to load also the JSON as a string?
HINT: just for test puposes, I noticed that if in the JSON I put a '\' before every '"' in the log, the loading runs smoothly, but unfortunately I cannot modify the content of the log.
Not without modifying the file beforehand - or writing your own UDParser function.
It clearly is a strange format: CSV (well, semicolon delimited and with double quotes as string enclosers), until the children appear - which are stored with a leading double quote and a trailing double quote; doubly nested with curly braces - JSON type, ok. But you have double quotes (not doubled) within the JSON encoding - any parser would go astray on those.
You'll have to write a program (ideally in C) to remove the curly braces, to remove the column names in the JSON code and leave just a CSV line
So, from the line (the backslash at the end means an escaped newline, meaning that the three lines you see are actually one line, for readability)
"concorde-fe";"DETAILS.SHOWN";"1bcilyejs6d4w";"2017-01-31T00:00:04.801Z"; \
"2017-01-31T00:00:04.714Z"; \
"{"requestedFrom":"BUTTON","tripId":{"request":3003926837969,"mac":"v01162450701"}}"
you make (title line with column names, then data line)
col1;col2;col3;timestampz1;\
timestampz2;requestedfrom;tripid_request;tripid_mac
"concorde-fe";"DETAILS.SHOWN";"1bcilyejs6d4w";"2017-01-31T00:00:04.801Z"; \
"2017-01-31T00:00:04.714Z";"BUTTON";3003926837969;"v01162450701"
Finally, you'll be able to load it as a CSV file - and maybe you will have to then normalise everything again: tripIdseems to be a dependent structure ....
Good luck
Marco the Sane

How can I append an underscore to a string variable in PowerShell without it interpreting a carriage return in a .ps1 file?

How can I append a - or _ to a string variable in PowerShell it interpreting a carriage return inside a script file?
I would like the _* appended to the end of my $curUserID variable, so that I can then use this as a filter.
I have tried lots of ways to get this, but all of them result in a carriage return after my curUserID value when inside of a script file. It works fine when I do the same at a command line in PowerShell?!
Here is my code:
$curUserID = "28";
$filePrefix = "${curUserID}$('_*')";
when I run this from inside of a script file, I get the following
28
_*
But what I need is the following, which is what I get when I run the above at the command line.
28_*
$filePrefix = "${curUserID}_*"

uigetfile not pulling entire file name

I'm using uigetfile to upload my data. I've time stamped my data with a date. So a file I want to upload looks like Data-Dec01_11/45/35.txt The problem is uigetfile reads till the first "/" and then assumes that that is the end of the file name. Thus it pulls the file name Data-Dec01_11. But of course when I load that file it doesn't exist. How do I force uigetfile to pull the entire file name?
You cannot use slashes or backslashes in your file names, as they can be mistaken with the file separator, as in your case.
You can use ´regexpr´ to rename your files so they do not contain illegal characters, as explained in this trhead.
I copy here the code they suggest for your convenience (I've just added a slash and a backslash to the example string so you can see the results):
% these characters are allowed
legalchars = 'a-zA-Z0-9\-\ \_\.' ;
% illegal filename
A = 'Some#charac\ters$are(not&allowed/.txt'
% replace every other character with an underscore
B = regexprep(A,['[^' legalchars ']'],'_')