Using implicit path name argument in glob - python-3.x

I am using glob.glob() to read all the png files in a directory. This is what I am using now
for filename in glob.glob('D:\test_files\**\*.png',recursive=True):
...
Image files are kept in sub folders in test_files. The code will be used by someone else within another thousands of lines of codes. Hence, the directory should be defined somewhere else in the code as a string. Also I would like to change *.png by using another for loop. So, I need something like
for filename in glob.glob('imagepath'+'extension',recursive=True):
...
where imagepath is defined somewhere else in the code and extension is kept in a list. Can I use the glob.glob() method in this way?

The answer is here. I define the path like path = 'D:\test_files\**' and keep the extensions in a list like imgExtentions =['\*.jpg', '\*.png']. Then simply I use glob:
for extension in imgExtentions:
for filename in glob.glob(path+extension,recursive=True):

Related

removing the trailing extension from a string in Python3

I have the python script below to iterate over all files ending with 'mkv', and print the same string without the 'mkv' at the end.
But, instead it prints the original filename including the 'mkv', why??
files=os.system('find /media/radamand/230_GB -name *mkv')
for file in str(files):
converted_filename=file[0:-3]
print(converted_filename)
Your os.system call executes your find command, sends its output to your interpreter standard output stream (which is why you're seeing your matching files including the "mkv" at the end, as this output is not the result of your print function in your later code), and then simply returns the exit code.
So your files variable actually gets an assignment of the integer 0.
Your for loop then casts files from an int into a string ('0') and thus your for loop now actually means: "loop through each character of the string files" (there is only one however), which, in this case, due to your slicing of [:-3] on a string of only one character, evaluates as an empty string which gets passed to your print function.
So, os.system isn't designed for what you are trying to achieve.
If you potentially have other folders in the parent folder you are searching, that may also have the filenames you are looking for, then I would recommend using the glob module.
import glob
files = glob.glob("/media/radamand/230_GB/*mkv") # Returns a list of strings for matched files
for file in files:
print(file[:-3])
You can add and set the keyword arguments recursive and/or include_hidden to True if required.
If, however, you are only looking for the files in the current folder, you can use fnmatch:
import fnmatch
import os
for file in os.listdir("/media/radamand/230_GB"):
if fnmatch.fnmatch(file, "*mkv"):
print(file[:-3])

NodeJS&Glob: exclude arbitrary files with arbitrary filename extensions from glob selections in one string

How can I exclude arbitrary file(s) with arbitrary filename extension from glob
selection?
For example: we need all .pug and .haml files in C:\Users\User1\projectname\src\markup
except index.pug (but index.haml is O'K).
The glob
'C:\\Users\\User1\\projectname\\src\\markup\\!(index).+(pug|haml)'
excludes both index.pug and index.haml - this is not that we want, and also there is no enougnh flexibility.
Let's make clear:
In this question we don't need to receive files by glob - the target is just create single glob selection. So the solutions like globby(['C:\\Users\\User1\\projectname\\src\\markup\\*.+(pug|haml)', '!C:\\Users\\User1\\projectname\\src\\markup\\index.pug']); are
not matches to this question.
If it's impossible - to exclude arbitrary file(s) with arbitrary extension(s) from glob - please say such as.
I tried
minimatch(
'C:\\Users\\bokovgleb\\projectname\\src\\src\\index.pug',
'C:\\Users\\bokovgleb\\projectname\\src\\src\\*(*.pug|!(index.pug))'
)
based on comment to this answer. It returns true...
For example: we need all .pug and .haml files in C:\Users\User1\projectname\src\markup except index.pug (but index.haml is O'K).
I came up with
/markup/!(*index){.pug,.haml,index.haml}
You can test it using globster.xyz

Using the glob module in while loop to wait for a download

I want to pause my python script while waiting for a file download to happen.
I don't want to use an explicit wait.
I want this to run fast and not rely on an explicit wait.
I'm kinda noobish but here is what I have tried.
file_check = glob.glob1('downloads', '*.pdf')
while not os.path.isfile(str(file_check)):
time.sleep(1)
I used the str() because it complained about needing a string for the path.
I have a feeling this isn't the way to properly do this. so how should I dynamically wait for a file download?
P.S
My .pdf file downloads into '/downloads', and my pdf is dynamically named before download so that's why I need globs wildcard.
When you do file_check = glob.glob1('downloads', '*.pdf') the result of glob.glob1(...) is stored in file_check just once and that's it. In this case, if you enter inside the while loop you never will get out of there because file_check will not change (except if there are threads or stuff like that that can modify their value externally).
glob.glob and glob.glob1 (this one it's not even public, as you can see the docs) returns a list. If 'downloads' folder it's empty, you will get an empty list []. In Python, lists have an implicit booleanness, so if the list it's empty, in a conditional statement it will be interpreted as False, or 'True' if it's not empty.
Rewriting your code the result will be something like this:
while not glob.glob1('downloads', '*.pdf'):
time.sleep(1)

Returning Filename with wildcards in VFP 9

I am trying to locate the full name of a file using a wildcard. The code I have is:
MLCNo=crjbis.ffromlot
subfolder=LEFT(mlcno,3)
filename=SYS(2000,'S:\MLC\MLC#\'+subfolder+'xx\'+mlcno+'21391*.pdf')
pathname="S:\MLC\MLC#\"+subfolder+"xx\"+filename
Pathname is passed to a print function to print the file. All of this works great if I don't use a variable in the SYS function (even with a wildcard). I should add that there will only ever be one file returned by the wildcard. Is there another way to do this?
Thanks!!!
Tammy

How can I copy picture and text from one file into another file dynamically using node.js

Suppose i have two files a_b_c_d.txt and e_f_g_h.png S,At runtime i.e., by using command prompt i have to create b folder inside that c folder inside that d folder inside that a.txt and same also for another file f->g->h->e.png and i have some text in a and image in epng . .So,how can I get values from those existing file into created files. .
You can find all the file system operations inside the fs module. http://nodejs.org/api/fs.html
But like tapan says if you need to do complex synchronous execution that manipulates the file system something like Bash will be a lot better suited for that.
So if I'm understanding you correctly you want to take a file named "a_b_c_d.txt" in some folder, and move that into a nested folder as:
./a_b_c_d.txt -> ./b/c/d/a.txt
The general solution would be:
Grab the file name using process.argv if it varies.
For example, if you supply the file as an argument to node, e.g.
node move.js "a_b_c_d.txt", the argument, "a_b_c_d.txt", will be in the argv array.
Process the file name using a combination of string and array methods.
Nodes current directory is stored in the __dirname global variable
if you need it.
You can split the extension from the rest of the path
using string's split(...) method.
For the above argument, split('.') will result in the array ['a_b_c_d', 'txt']
You can then split 'a_b_c_d' using '_',
and use various array operations to pull the file name 'a'
out of the array, so that you're left with the path ['b', 'c', 'd']
and the file name and extension sitting in their own variables somewhere.
Use fs.mkdirSync(...) on the path array to make each nested folder,
starting with b (e.g. using array's forEach(...) method).
You could also use the async fs.mkdir(...) and supply callbacks,
but the sync version is easier in this case.
Finally use fs.renameSync(...) to move ./a_b_c_d.txt to ./b/c/d/a.txt.
As you can see, python or bash (as tapan suggested) would probably be simpler for this use case, but if for some reason you have to use node, the above advice will hopefully be enough to get you started.

Resources