Make a List for sub directories path [duplicate] - python

This question already has answers here:
Getting a list of all subdirectories in the current directory
(34 answers)
Closed 7 years ago.
I have a directory and I need to make a path list for the sub directories.
For example my main directory is
C:\A
which contains four different sub directories : A1,A2,A3,A4
I need a list like this:
Path_List = ["C:\A\A1","C:\A\A2","C:\A\A3","C:\A\A4"]
Cheers

import os
base_dir = os.getcwd()
sub_dirs = [os.path.join(base_dir, d) for d in os.listdir(base_dir)]

You could (and should, i think) use os module, wich is easy to use and provides a lot of stuff to deal with paths.
I wont say anymore, because your question is vague and shows no effort on searching. You are welcome!!
sub_dirs = os.listdir(os.getcwd()) ## Assuming you start the script in C:/A
path_list = []
for i in sub_dirs:
path_list.append(os.path.join(os.getcwd(),i))
Dirty and fast.

Related

List the content of a folder using python [duplicate]

This question already has answers here:
How can I list the contents of a directory in Python?
(8 answers)
Closed 4 years ago.
Is it possible to output the contents of a folder in python ?
If, say you had a function and you would pass the folder path to its argument?
What would be the simplest way for a beginner to take?
By "simplest" I mean, without using any modules.
Note
Google usually leads me to stackoverflow. Plus I am not looking for generic answers. I am looking for insight based on experience and wit, which I can only find here.
You could use os.listdir() to look at the contents of a directory. It takes the path as an argument, for example:
import os
directory = os.listdir('/')
for filename in directory:
print(filename)
prints this for me:
bin
home
usr
sbin
proc
tmp
dev
media
srv
etc
lib64
mnt
boot
run
var
sys
lib
opt
root
.dockerenv
run_dir
How about listdir? Let's try like this way-
import os
def ShowDirectory:
return os.listdir("folder/path/goes/here")
ShowDirectory()
With function.
def ListFolder(folder):
import os
files = os.listdir(folder)
print(*files,sep="\n")
Please use google first next time...it seems you are looking for this:
How can I list the contents of a directory in Python?
The accepted answer there:
import os
os.listdir("path") # returns list

Use os.listdir to show directories only [duplicate]

This question already has answers here:
How to list only top level directories in Python?
(21 answers)
Closed 2 years ago.
How can I bring python to only output directories via os.listdir, while specifying which directory to list via raw_input?
What I have:
file_to_search = raw_input("which file to search?\n>")
dirlist=[]
for filename in os.listdir(file_to_search):
if os.path.isdir(filename) == True:
dirlist.append(filename)
print dirlist
Now this actually works if I input (via raw_input) the current working directory. However, if I put in anything else, the list returns empty. I tried to divide and conquer this problem but individually every code piece works as intended.
that's expected, since os.listdir only returns the names of the files/dirs, so objects are not found, unless you're running it in the current directory.
You have to join to scanned directory to compute the full path for it to work:
for filename in os.listdir(file_to_search):
if os.path.isdir(os.path.join(file_to_search,filename)):
dirlist.append(filename)
note the list comprehension version:
dirlist = [filename for filename in os.listdir(file_to_search) if os.path.isdir(os.path.join(file_to_search,filename))]

Automatically creating a list in Python [duplicate]

This question already has answers here:
How do I list all files of a directory?
(21 answers)
Closed 8 years ago.
My code:
red_images = 'DDtest/210red.png'
green_images = 'DDtest/183green.png'
b = [red_images, green_images]
shuffle(b)
I have several hundred pictures, and in hopes of making my code as concise as possible (and to expand my python knowledge), I was wondering how I write code that automatically takes the files in a folder and makes them a list.
I have done similar things in R, but I'm not sure how to do it in python.
You can also use os:
import os
from random import shuffle
# Base directory from which you want to search
base_dir = "/path/to/whatever"
# Only take the files in the above directory
files = [f for f in os.listdir(base_dir)
if os.path.isfile(os.path.join(base_dir, f))]
# shuffle (in place)
shuffle(files)
import glob
my_new_list = glob.glob("/your/folder/*")

How Could Python iterate the directoriy and detect the new files? [duplicate]

This question already has answers here:
Monitoring contents of files/directories? [duplicate]
(5 answers)
Closed 8 years ago.
How can I iterate all files and subdirs in a Dir,
and can detect new file when put there?
Thank you for your help!
Try os.walk. More specifically, try:
top="."
import os
for root, dirs, files in os.walk(top):
for name in files:
# do something with each file as 'name' (a)
pass
for name in dirs:
# do something with each subdir as 'name' (b)
pass
# do something with root (dir path so far)
# break at any point if necessary
To answer the question in your comment, at point (b) in the code, you can handle any subdirectory logic (also you can check to test that you have the right subdirectory to do certain custom logic on), via another function or directly/inline.

save os.walk() in variable [duplicate]

This question already has answers here:
os.walk() ValueError: need more than 1 value to unpack
(4 answers)
Closed 9 years ago.
Can I somehow save the output of os.walk() in variables ? I tried
basepath, directories, files = os.walk(path)
but it didn't work. I want to proceed the files of the directory and one specific subdirectory. is this somehow possible ? Thanks
os.walk() returns a generator that will successively return all the tree of files/directories from the initial path it started on. If you only want to process the files in a directory and one specific subdirectory you should use a mix of os.listdir() and a mixture of os.path.isfile() and os.path.isdir() to get what you want.
Something like this:
def files_and_subdirectories(root_path):
files = []
directories = []
for f in os.listdir(root_path):
if os.path.isfile(f):
files.append(f)
elif os.path.isdir(f):
directories.append(f)
return directories, files
And use it like so:
directories,files = files_and_subdirectories(path)
I want to proceed the files of the directory and one specific
subdirectory. is this somehow possible ?
If that's all you want then simply try
[e for e in os.listdir('.') if os.path.isfile(e)] + os.listdir(special_dir)

Categories