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
#6
Hi Shakir,

What I was saying is that it can be better to avoid the chdir altogether. Changing the current directory is practical for dirty code in the command line and some really obscure special occasions, but normally is just a source of bugs...

This code is what I manage to understand from tour explanation (maybe I got wrong the idea of what you want to do, but look to the tools to play with paths and adapt it to your code)
#!/usr/bin/python3
 
import os
import shutil
import sys
import fnmatch
 
top_dir = "/home/mmcneil/Desktop/TMP"
root_dir = [d.name for d in os.scandir('.') if d.is_dir()]
 
mapping = {
    'Blaster': 'blaster*', 'Clash': 'clash*', 'Force': 'force*',
    'Lockup': 'lockup*', 'PowerOFF': 'p*w*off*',
    'PowerON': 'p*w*on*', 'Sign1': 'combo*', 'Spin': 'spin*', 
    'Stab': 'stab*', 'Swing': 'swing*'
}

## Create for all the folders in the current directory a set of subdirectories
#  So if the current directory has the folders a, b, c everything finish as
#   ./a/Blaster    ./b/Blaster   ./c/Blaster
#   ./a/Clash      ./b/Clash     ./c/Clash
#   ./a/...        ./b/...       ./c/...
for root_folder in root_dir:
    for section in mapping:
        os.makedirs(os.path.join(root_folder, section), exist_ok=True)
## Everthing ABOVE this line works the way I want it to##
 
## Try to move all the files that are under top_dir/<Folder>/<glob> to
## ./<Folder>/<section>/
for ret_folder in root_dir:
    
    source = os.path.join(top_dir, ret_folder)
    # List all the files in the source
    for entry in os.scandir(source):
        if not entry.is_file():
            continue
        for section, pattern in mapping.items():
            if not fnmatch.fnmatch(entry.name, pattern):
                continue
            # Move top_dir/<Folder>/file to ./<Folder>/<section>/
            ori = os.path.join(source, entry.name)
            dst = os.path.join('.', ret_folder, section)
            shutil.move(ori, dst)
            break
I think the problem in your code was in the line
for file in os.path.join(top_dir, ret_folder):
That does not list all the files in the directory "<top_dir>/<ret_folder>" but iterate over the characters in the string (so file was "/", "h", "o", "m", "e"...)
That is one of the advantages of python (you can iterate in almost anything) but sometimes is not what you want to do.
To solve this type of problems the best thing is to put some traces in your code or use a debugger to put a breakpoint before the loop.
Reply


Messages In This Thread
RE: First time with Python.. need help with simple script - by killerrex - May-04-2018, 10:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,469 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,390 May-10-2023, 02:13 PM
Last Post: Calab
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,683 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Clock\time calculation script Drone4four 3 1,519 Jan-21-2022, 03:44 PM
Last Post: ibreeden
  Simple Python script, path not defined dubinaone 3 2,753 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Real-Time output of server script on a client script. throwaway34 2 2,111 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  PyCharm Script Execution Time? muzikman 3 8,589 Dec-14-2020, 11:22 PM
Last Post: muzikman
  Need help creating a simple script Nonameface 12 4,728 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,024 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,365 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