How do I list all files and directories in a directory in Julia? - directory

In Python, I could use:
from os import listdir
os.listdir()
In Julia, how can I list everything inside a given directory?

readdir()
This lists all files and in a current directory folder as Vector{String}
Of course you can provide directory name:
readdir("dir/name")
On Windows a useful practice is raw String macro:
readdir(raw"c:\temp")
If you want to see what is in subdirectories use walkdir:
items = [item for item in walkdir("some/path")]
This walks through whole directory tree and returns a Vector of 3-element tuples where first element is directory, second list of it subdirectories and the third the list of files in a directory.

Related

how to write a function dir_info(directory) which returns 3 values

(1) Number of files inside the directory (recursively)
(2) Number of directories inside the directory (recursively)
def dir_info(directory):
nfiles = how many files inside directory?
ndirs = how many sub directories inside directory?
return nfiles, ndirs

how get count of parent Directories from zip (Compressed) file in qt c++?

Im new in qt. I use quazip for compress a folder and it compressed successfully.
But now if i want to know how many parents dir is available in unknown zip?
how can i get count of parent dir from zip file? before unzip.
please help me to get count of dir.
Is it possible by quazip? how?
QuaZip::getFileInfoList() should give you all the information needed. For the number of files just use the length of the returned list. Note that the file info is listed recursively. In case you need only the non-recursive content of a directory, go to that directory and iterate over all files with goToFirstFile() / goToNextFile().
I found the answer to my question:
Quazip zip("ZipFileName");
QuazipDir DirInZip(&zip);
int count = DirInZip.setFilter( QDir::Dir | QDir::NoDotAndDotDot|QDir::NoDot ).count();

How to get the address of a Julia script file within itself?

I have a test.jl Julia script file in a directory, e.g., /home/directory1. Assume that I want to save the output of this code as a .txt file in the same directory. My problem is that, obviously, the .txt file will be saved in the working directory of Julia, which by pwd() is somewhere else, e.g., /home/julia/.
I wonder how can I change the working directory in the test.jl script WITHOUT writting the directory address, /home/directory1 , manually? I know that I can change the working directory by cd("/home/directory1"), but the problem is that I don't want to write the address manually. Is there any way to get the directory address of test.jl within itself by a command?
You can get that information using macro #__FILE__
help?> #__FILE__
#__FILE__ -> AbstractString
Expand to a string with the path to the file containing the macrocall, or an empty string if evaluated by julia -e <expr>. Return nothing if the macro was missing parser source information. Alternatively see PROGRAM_FILE.
Example:
julia> open("c:\\temp\\some.jl","w") do f
println(f, "println(\"Running at \$(#__FILE__)\")")
end
julia> include("c:\\temp\\some.jl")
Running at c:\temp\some.jl
shell> julia "c:\temp\some.jl"
Running at c:\temp\some.jl

Return one folder above current directory in Julia

In Julia, I can get the current directory from
#__DIR__
For example, when I run the above in the "Current" folder, it gives me
"/Users/jtheath/Dropbox/Research/Projects/Coding/Current"
However, I want it to return one folder above the present folder; i.e.,
"/Users/jtheath/Dropbox/Research/Projects/Coding"
Is there an easy way to do this in a Julia script?
First, please note that #__DIR__ generally expands to the directory of the current source file (it does however return the current working directory if there are no source files involved, e.g when run from the REPL). In order to reliably get the current working directory, you should rather use pwd().
Now to your real question: I think the easiest way to get the path to the parent directory would be to simply use dirname:
julia> dirname("/Users/jtheath/Dropbox/Research/Projects/Coding/Current")
"/Users/jtheath/Dropbox/Research/Projects/Coding"
Note that AFAIU this only uses string manipulations, and does not care whether the paths involved actually exist in the filesystem (which is why the example above works on my system although I do not have the same filesystem structure as you). dirname is also relatively sensitive to the presence/absence of a trailing slash (which shouldn't be a problem if you feed it something that comes directly from pwd() or #__DIR__).
I sometimes also use something like this, in the hope that it might be more robust when I want to work with paths that actually exist in the filesystem:
julia> curdir = pwd()
"/home/francois"
julia> abspath(joinpath(curdir, ".."))
"/home/"

find command: conditionally prune subdirectories of any directory containing a matched file

I'd like to search a directory tree for regular files matching particular patterns, e.g.
find -regex '.*\.\(expect\|missing\|ok\)'
This is fine, but in my application it is very inefficient -- since if a directory contains at least one matching file, then none of its (many, deep, file-laden) subdirectories need to be searched, since they definitely won't contain any matches.
So how do I -prune subdirectories, only if a matched file is found in their parent directory?
E.g. if I have the following tree:
./a/file.ok
./a/a1/
./a/a2/
./b/file.missing
./b/file.expect
./b/b1/
./b/b2/
./c/c1/
./c/c1/c11/
./c/c1/c11/foo.expect
./c/c1/c11/foo.txt
Then I would like to print only:
./a/file.ok
./b/file.missing
./b/file.expect
./c/c1/c11/foo.expect
without actually searching/traversing all the subdirectory trees of a/ and b/
(This earlier question approaches what I want, but hasn't yet really been answered.)

Resources