Find a specified folder within a whole partition [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to find a specified folder which can be located anywhere on the disk.
I wonder what would be the fastest solution to do this with a Python 3.4.
I know folder name, for example "XXX" and it's subfolder "YYY". And for not to be easy, there are many folders called "XXX" but none of them contains "YYY" folder. So it's quite unique.
I wanted to walk on C: and find folder "XXX" and if it is found then check if it contains "YYY".
But maybe there is some kind of library which can speed this up or something?

import os
partition = input("Which drive do you want to search? ")
dirname = "XXX"
subdirname = "YYY"
for root, dirs, _fnames in os.walk(partition):
if os.path.basename(root) != dirname: continue
if not os.path.isdir(os.path.join(root, subdirname)): continue
print("Found required folder:", root)
break

Related

Iterating through images in folder path [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have:
flags = ['australia.png', 'canada.png', 'newzealand.png', 'uk.png', 'usa.png']
and if these images are in some folder called "flags", how can I set the path to that folder to iterate through them?
You need to include the path to the flags folder which can be done using the os module. This will be more robust and portable than adding them as strings.
import os
flag_folder = '/path/to/flags'
flags = ['australia.png', 'canada.png', 'newzealand.png', 'uk.png', 'usa.png']
for filename in flags:
flag_path = os.path.join(flag_folder, filename)
# do something with flag_path
In case you want something to happen just when the images are in the flags folder:
import os
path = '.../flags/'
for flag in flags:
if os.path.exists(path + flag):
*do something*

Looping over subdirectories and their subdirectories and creating a dictionary [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How would I loop over subdirectories in a directory, returning them like this:
dirlist = {
"area": ["square","circle"],
"line": ["line"]
}
There's no need for fancy stuff, because I'm sure that there's only a subdirectory and a sub-subdirectory, nothing past that.
There are multiple subdirectories and multiple sub-subdirectories for each subdirectory.
Link to half solution.
The actual walk through the directories works as you have coded it. If
you replace the contents of the inner loop with a simple print
statement you can see that each file is found:
import os
rootdir = 'C:/Users/sid/Desktop/test'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print os.path.join(subdir, file)
If you still get errors when running the above, please provide the
error message.
Updated for Python3
import os
rootdir = 'C:/Users/sid/Desktop/test'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print(os.path.join(subdir, file))
Other half is simple, you don't find a file as given in the link, you juts record the names of the subdirs in arr and the main subdir in as a key in dict, then to
make the arr the value of key.

How to delete everything in a folder except one or two folders that I want to retain? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have folder "Folder1" that contains files and folders such as "file1", "file2" "Folder11" "Folder12" "Folder13" etc. I want to retain only "Folder11" & "Folder12" and delete rest of the things. I need help to write python script for the same.
You can use the os module to loop through every item and folder in your directory and delete them. You could also define a list with specific items (like your folder names) to prevent them from being deleted. Keep in mind that if you want to keep files from being removed, you also have to add their format (like ".txt" for a text file).
As usual, be careful when deleting files. Maybe you want to add a check before the os.remove() or maybe replace it with a print statement first (print(item)) so that you see what is being removed.
import os # Import the os module
working_directory = r"C:\Users\Vishwesh\Folder1"
retain = ["Folder11", "Folder12", "file1.txt", "file2.txt"]
os.chdir(working_directory) # Change directory to your folder
# Loop through everything in folder in current working directory
for item in os.listdir(os.getcwd()):
if item not in retain: # If it isn't in the list for retaining
os.remove(item) # Remove the item

What is the meaning of "./" in os.path? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to search a file in my directory files. I saw an example and couldn't understand:
import os
import glob
file_glob = './rpts/folderA/*_fileA.txt'
magic_check = re.compile('[*?[]')
if(magic_check.search(file_glob))
file_list = glob.glob(os.path.expanduser(file_glob))
What does the ./ part mean? I understand that ../ is switch to previous dir.
What I think it does:
expand the wild card to get a list of files that matches the regex
The files are stored in a list called file_list
Magic check regex, [*?[]: What is the [ inside [ ] for?
As Martijn said, this is UNIX shell notation for the current location (cwd or pwd). The reason it's in the command is to be more robust. If the user's environment doesn't have "./" in the search path ($PATH variable), then the shell won't find the file rpts/folderA/*_fileA.txt. With "./" at the front, this script is independent of $PATH.

How to get all files from a directory and display them on a list in tkinter - Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is a bit hard to explain but imma do my best.
Imagine a have a dir ( ~/.profiles )
In this dir there are two files ( fileone.msp and filetwo.msp )
I assign these files to a variable with
from os import listdir
from os.path import isfile, join
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
Now I want to have a button for each of these files with the name of the file found.
So lets say the output is files = ["fileone.msp", "filetwo.msp"] , now I want a tkinter window with two button with one saying fileone.msp and another saying filetwo.msp.
In case it was three files ( third being called filethree.msp ) I wanted three buttons each one with names and so on and so forth.
Thanks! I hope I explained my self.
If I understand the question correctly (and I doubt that I do...), to create buttons from a list you simply iterate over the list like you would for any other task:
for file in files:
button = tk.Button(root, text=file, ...)
button.pack(...)

Categories

Resources