Python Forum
How to loop in python over subdirectories and copy the output to other subdirectories
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to loop in python over subdirectories and copy the output to other subdirectories
#1
I have a Jenkinsfile with the below code in one of the stages:

sh """
    python funcs/echo/tests/test.py || true
    coverage xml funcs/echo/tests/test.py || true
"""
My folder structure is:

Output:
|-- funcs | |-- echo | | |-- app.py | | `-- tests | | `-- test.py | |-- moshe | | |-- app.py | | `-- tests | | `-- test.py | `-- sum | |-- app.py | `-- tests | `-- test.py `-- tests |-- echo | `-- tests |-- moshe | `-- tests `-- sum `-- tests
How can I loop using python3 thought the funcs folder? I tired to run the below:

my_path="funcs"
for dirs in os.walk (my_path):
    print (dirs)
but it return the below:

Output:
funcs funcs/moshe funcs/moshe/tests funcs/echo funcs/echo/tests funcs/sum funcs/sum/tests
but what I really need is to loop on those:

Output:
funcs/moshe/tests funcs/echo/tetss funcs/sum/tests
and then for each one copy the 2 output files to the target I described above
Reply
#2
I don't understand your output at all. The code you show should be printing tuples with three elements, not strings...

import os
my_path="tree"
for dirs in os.walk(my_path):
    print (dirs)
Output:
('tree', ['Beta', 'Theta', 'Alpha'], ['Tutorial.webm', '_K24223951.mkv', 'walker2.py', 'Intros.mp4', 'print_chars.py', 'walker.py', '03 Using The Command Line.mp4', 'Binary to CAPITAL.MP4', '01 Course Welcome.mp4', 'Intro.mp4', 'Binary to small.mp4', 'Audience.mp4']) ('tree/Beta', ['beta1'], ['Decimal to octal conversion.mp4', 'Decimal to binary conversion.mp4']) ('tree/Beta/beta1', [], ['Data Science Roles.mp4', 'Will Data Science be on Demand.mp4', 'Why do we need Data Science.mp4', 'What is Data Science.mp4']) ...
The first component is the directory you are in right now. The third component is the list of files in that directory. So examine the first one (dirpath) and see if the final component is "tests". If so, copy the files from that directory to where you want.
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy documents to Teams using python SallySmith 0 2,339 Mar-23-2021, 04:27 AM
Last Post: SallySmith
  Moving Files From Subdirectories To Another Directory Harshil 5 3,908 Oct-06-2020, 10:52 AM
Last Post: ndc85430
  using 'while loop' output going into infinite loop... amitkb 2 1,910 Oct-05-2020, 09:18 PM
Last Post: micseydel
  sports Stats > table output loop problems paulfearn100 3 2,449 Jul-22-2020, 03:21 AM
Last Post: c_rutherford
  Print output in single file using pramika loop deepakkhw 1 2,037 Jul-11-2020, 11:57 AM
Last Post: j.crater
  To Copy text values to Excel using Python ksasi2k3 16 15,557 Dec-26-2019, 11:59 AM
Last Post: ashutoshdeodhar
  Output in for LOOP iterated Renym 1 1,480 Nov-17-2019, 09:15 PM
Last Post: perfringo
  Copy data from different workbooks into Master sheet with Python Fatman003 0 2,184 Aug-27-2019, 07:36 AM
Last Post: Fatman003
  How do I copy files faster with python? steckinreinhart619 7 18,161 Jul-19-2019, 11:47 AM
Last Post: perfringo
  Making a generalised CSV COPY script in Python Sandy7771989 3 2,379 Jul-05-2019, 11:02 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020