Reputation: 350
I am searching for a specific directory (/android).
I know that in python I can walk directories with os.walk(root_dir) but the problem here is that I do not know if the directory that I am looking for is a child of root_dir or if its a parent directory of root_dir.
Is there any method that perform the same operation than os.walk() but in the opposite way?
Thanks.
Upvotes: 0
Views: 165
Reputation: 350
I solved this issue with this code:
def copy_my_custom_class(current_dir):
subdirs = os.listdir(current_dir)
for subdir in subdirs:
if (subdir == 'android'):
dest_file_path = os.path.join(current_dir, subdir, 'MyCustomClass.smali')
shutil.copyfile('./files/MyCustomClass.smali', dest_file_path)
return 0
copy_my_custom_class(os.path.dirname(current_dir))
Upvotes: 0
Reputation: 350
Here is the solution that I chose however I have an error when parsing the absolute path to copy the file. I get this error, I think it is because I need absolute path, does someone know how to get an absolute path from a directory list?
FileNotFoundError: [Errno 2] No such file or directory: 'android\\MyCustomClass.smali'
here is my code:
def copy_my_custom_class(current_dir):
subdirs = os.listdir(current_dir)
for subdir in subdirs:
if (subdir == 'android'):
my_custom_class_path = os.path.join(subdir, 'MyCustomClass.smali')
shutil.copyfile('./files/MyCustomClass.smali', my_custom_class_path)
copy_my_custom_class(os.chdir(current_dir))
Upvotes: 0
Reputation: 1857
You can go to parent directory of root_dir
by using ..
in os.path.abspath()
.
import os
parent_dir = os.path.abspath(os.path.join(root_dir, ".."))
Now you have parent_directory
of root_dir
in parent_dir
, You can make it root_dir
and use os.walk(root_dir)
again.
Upvotes: 2