show all files under a dirctory - file

import os
def findFiles(root_dir="C:/Users/15025/Desktop/books/", res=None):
if not res:
res = []
for f in os.listdir(root_dir):
f_path = os.path.join(root_dir, f)
if os.path.isfile(f_path):
res.append(f_path)
elif os.path.isdir(f_path):
findFiles(f_path, res)
return res
result = findFiles()
print(result)
I find some problems here, if I have a file under "C:/Users/15025/Desktop/books/" directory, like deepLearning, and in this deepLearning file I have a document named debug.py. But when I run this program, the print(result)shows nothing, it is strange. Could someone tell me why? I think it is because the program will be called twice. But what is the reason in detail?
The figure of my directory and the running result could seen below:

WTF, I feel I always answer my own question, finally, I find that I should change findFiles(f_path, res) to res = findFiles(f_path, res), and then everything is fine.

Related

Returning a file handler from function + yield issue

2 questions relating to the same part of the code - I have this function:
def get_pdf(file_name: str):
with open(f"{PDFS_LOCATION}/{file_name}") as f:
yield f
I am using yield so the context manager will work well. However this creates 2 problems:
I'm not sure if a function that yielded instead of returned, what should it say it's return type is
If I decide to return a file handler, what should the return type be?

UITextChecker code not giving correct result

I am using the standard method I think to check a word for validity. I have a 4 part array that is joined to make the word being verified.
I have this placed in my touches began func in a SpriteKit scene however it always gives me the answer "CORRECT SPELLING" even when it is not.
Curiously when I put the exact same bit of code into my did move to view func so it runs when the game starts it works perfectly well. The code is exactly the same except instead of the joined array I just create a let word = "WORD" before the code. So I am guessing the problem must be something to do with my joined array??
This is my code
let word = wordArray.joined()
let textChecker = UITextChecker()
let misspelledRange = textChecker.rangeOfMisspelledWord(
in: word, range: NSRange(0..<word.utf16.count), startingAt: 0, wrap: false, language: "en_US")
if misspelledRange.location != NSNotFound,
let guesses = textChecker.guesses(
forWordRange: misspelledRange, in: word, language: "en_US")
{
print("Guess: \(String(describing: guesses.first))") //Output is: Guess: Optional("Tester")
} else {
print("\(word) CORRECT SPELLING")
}
Turned out my issue was that UITextChecker does not like full caps!
Only accepts lower case spellings

Windows Defender API to scan a directory for malwares

Using Windows Defender API , I'm trying to do a scan for malwares on a folder.
Following The documentation I wrote the code:
MPRESOURCE_INFO ResourceInfo = { 0 };
MPSCAN_RESOURCES ScanResource = { 0 };
PMPRESOURCE_INFO ResourceInfoArray = NULL;
...
ResourceInfo.Scheme = L"dir";
ResourceInfo.Path = L"C:\\temp";
ResourceInfo.Class = 0;
// ResourceInfoArray was Allocated before
*ResourceInfoArray = ResourceInfo;
ScanResource.dwResourceCount = 1;
ScanResource.pResourceList = ResourceInfoArray;
// Opened hMpManager before using MpScanStart
hRetval = MpScanStart(hMpManager, MPSCAN_TYPE_RESOURCE, 0, &ScanResource, NULL, &ScanHnadle);
From which I get an error message: An unexpected problem occurred. Install any available updates, and then try to start the program again. For information on installing updates, see Help and Support.
However If I change the ResourceInfo definition to:
ResourceInfo.Scheme = L"file";
ResourceInfo.Path = L"C:\\temp\\MyFile.exe";
ResourceInfo.Class = 0;
It works great, detecting the file in the right way.
On the bottom line - the code works for files, but doesn't work for directories.
Does anyone know what am I doing wrong with the directory search?
Analyzing event logs created by MpCmdRun.exe I found out that it uses the scheme "folder" instead of "dir". That change made my code working.
ResourceInfo.Scheme = L"folder";
Folder paths do not have to end with backslash, but drives require it: (F:\).

Trouble formatting a path for the Linphone SDK in Swift

Thanks in advance for the help!
I am trying to record calls using the Linphone SDK in Swift on Mac OS, and am having trouble passing a path into the function:
func linphone_call_params_set_record_file(_ cp: OpaquePointer!, _ path: UnsafePointer<Int8>!)
that works correctly (the SDK is written in C, though I am accessing it using Swift and a bridging header). The Linphone SDK works properly, and I can make and receive calls programmatically, with full audio support.
In trying to invoke the call recorder, I pass this function a path (pathtofile), such as:
let pathtofile = "/Users/Alex/Safety/1.wav"
where I would like to store the recording file.
func SafetyNetAVRecorderInitializer(pathtofile: String) -> Bool {
// Convert pathtofile to UnsafePointer<Int8>.
let cpathtofile = (pathtofile as NSString).utf8String
let path = UnsafeMutablePointer<Int8>(mutating: cpathtofile)
// Actually begin call recording.
if currentcall != nil {
let currentcallparameters = linphone_call_get_current_params(currentcall)
linphone_call_params_set_record_file (currentcallparameters, path)
linphone_call_start_recording(currentcall)
return true
}
return false
}
No runtime errors are encountered on linphone_call_params_set_record_file(), but when I try to invoke linphone_call_start_recording(), the recording does not begin, and an error is printed in the console that reads:
ortp-error-linphone_call_start_recording(): no output file specified. Use linphone_call_params_set_record_file().
How can I correctly pass a valid path to linphone_call_params_set_record_file()? I have tried directly passing a plain Swift String instead of an UnsafePointer<Int8> to no avail. Am I just misunderstanding how paths are formatted in C?
For reference, the SDK method source is:
void linphone_call_params_set_record_file(LinphoneCallParams *cp, const char *path){
if (cp->record_file){
ms_free(cp->record_file);
cp->record_file=NULL;
}
if (path) cp->record_file=ms_strdup(path);
}
Thanks again!
Try this:
let cpathtofile = (pathtofile as NSString).utf8String! // Unwraps!
...
inphone_call_params_set_record_file(currentcallparameters, cpathtofile)

How to call an m file from another m file in MATLAB and retrieve an output?

I know this is a simple question, but for some reason I can't find a straight answer that works no matter where I look.
Basically, I have 4 values that were found in one m file, and I want to run them through a separate m file and retrieve the output from it.
I tried something like these, but none worked:
result = generate(nrow,ncol,a,b);
function result = generate(nrow,ncol,a,b);
result = #generate(nrow,ncol,a,b);
The final value in the m file "generate" is called result, and I'm trying to carry that across to my initial m file.
Any advice as to what I'm doing wrong would be greatly appreciated! Please and thank you
if your file generate.m defines a function it should have itself the following structure (which takes into account the fact that you have four returned values)
function [ret1 ret2 ret3 ret4] = generate(nrow,ncol,a,b)
.... % # Some processing of yours
ret1 = ... ; % # Returned values are eventually set
ret2 = ... ;
ret3 = ... ;
ret4 = ... ;
end
The function should be called (e.g. in your main script) as
[ret1 ret2 ret3 ret4] = generate(nrow,ncol,a,b);
now you have the variables ret1,ret2,ret3,ret4 available in the caller scope.
Be aware that the file generate.m must be in the current matlab PATH.

Resources