Python Forum
First time with Python.. need help with simple script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First time with Python.. need help with simple script
#2
Hi,

You are using os.walk to access to the current directory, but remember that os.walk will transverse the entire folder tree. If you only want to list the current directory is enough with os.listdir or even better os.scanlist.
# List all the elements of the current directory that are folders
folders = [d.name for d in os.scandir('.') if d.is_dir()]
I think the problem is when you try to chdir in the 'top_dir/root_dir'... the last for loop (the one in the line 23) is supposed to work for each of the ret_folder values, but as it is outside of the for in line 19 it only runs for the last value and remember that os.walk returns the folders in no particular order.

As a small recommendation, try to chdir as less as possible. In your case all the loop can work without having to move the execution directory as shutil.move works fine with absolute paths (os.getcwd allows you to obtain the current folder full path).

Another nice trick to avoid repeating the same fnmatch schema is to store them in a dictionary. If it is {mask: Folder} that many masks can go to the same directory, if you use the opposite {Folder: mask} each folder can only contain one set of files.

mapping = {'Blaster': 'blaster*', 'Clash': 'clash*'} # And all the others...
# for file in os.listdir('.'):
for file in os.path.join(top_dir, ret_folder):
    for target in mapping:
        if fnmatch.fnmatch(file, mapping[target]):
            shutil.move(file, target)
Reply


Messages In This Thread
RE: First time with Python.. need help with simple script - by killerrex - May-03-2018, 10:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,454 Jun-29-2023, 11:57 AM
Last Post: gologica
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,381 May-10-2023, 02:13 PM
Last Post: Calab
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,675 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Clock\time calculation script Drone4four 3 1,518 Jan-21-2022, 03:44 PM
Last Post: ibreeden
  Simple Python script, path not defined dubinaone 3 2,751 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Real-Time output of server script on a client script. throwaway34 2 2,102 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  PyCharm Script Execution Time? muzikman 3 8,580 Dec-14-2020, 11:22 PM
Last Post: muzikman
  Need help creating a simple script Nonameface 12 4,718 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  How to kill a bash script running as root from a python script? jc_lafleur 4 6,008 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,348 May-28-2020, 05:27 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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