1

I want to ask is it possible to make a rules for file managing program. For instance, if i want to move certain file in Image folder to move only .png and .img files. How i should declare rule in the configuration file that program would understand? If you would need I would my code gladly upload it. Which libraries i would need to use or give certain examples? Thank you in advance!

from tkinter import *  
from tkinter import filedialog
import easygui
import shutil
import os
from tkinter import filedialog
from tkinter import messagebox as mb
from pathlib import Path
import logging
from datetime import date
import configparser
import configdot

def open_window():
    read=easygui.fileopenbox()
    return read

#logging config

if Path('app.log').is_file():
    print ("Log file already exist")
else:
    logging.basicConfig(filename='app.log', filemode="w", format='%(name)s - %(levelname)s - %(message)s ')
    print ("Log file doesn't exist, so new one was created")


LOG_for="%(asctime)s, %(message)s"

logger=logging.getLogger()
logger.setLevel(logging.DEBUG)


# move file function
def move_file():
    filename = filedialog.askopenfilename(initialdir = config.select.dir,
                                          title = config.select.ttl,
                                          filetypes = config.select.ft)

    destination =filedialog.askdirectory()
    dest = os.path.join(destination,filename)
    if(source==dest):
        mb.showinfo('confirmation', "Source and destination are the same, therefore file will be moved to Home catalog")
        newdestination = Path("/home/adminas")
        shutil.move(source, newdestination)
        logging.shutdown()
        logging.basicConfig(filename='app.log', filemode="a", format=LOG_for) 
        logging.info('File *' + filename + '* was moved to new destination: ' + newdestination)
        
    else:
        shutil.move(source, destination)  
        mb.showinfo('confirmation', "File Moved !")
        #current_time()
        logging.basicConfig(filename='app.log', filemode="a", format=LOG_for)
        logging.info('File *' + filename + '* was moved to new destination: ' + destination) 
                                                                                                  
# Create the root window
window = Tk()
Tomee
  • 129
  • 3
  • 16
  • what kind of file managing program? and what kind of rules, your question is too broad. – aSaffary May 04 '21 at 11:00
  • 1
    Are you creating such a file manager, or do you want to write rules for an existing one ? If the former is the case, you will need to show us the code. If the latter is the case, you will need to tell us which file manager (there are dozens of them) and an example of its configuration file. – TheEagle May 04 '21 at 11:03
  • Image folder to move only .png and .img files @aSaffary – Tomee May 04 '21 at 11:04
  • @Proggramer I have uploaded the code – Tomee May 04 '21 at 11:09

1 Answers1

2

It sounds like you want to move only files of a specific type (e.g. .png or .img). You can do this based on the file name itself using endswith:

if source.endswith('.png'):
   shutil.move(source, newdestination)

You can see an example in this answer Converting images to csv file in python where the function walks through a directory and creates a list of files that all share the same file extension.

Note that this code only checks the file extension, not the actual file itself.

Now, say you only want to apply this rule to a folder called "Images", then you can still use string manipulation. You might use contains:

if source.contains("Images"):
    # do something

Alternatively, if you know your file path might contain the word "Images" more than once, you can use os.path.split(path) to split the path into "path" and "file", and then again on the resulting head to split it into "path" and last folder. Compare the resulting tail to Images:

h, t = os.path.split(source)
h2, t2 = os.path.split(h)
if t2.contains("Images"):
    #do something

There are other ways to do this using the os.path functions and you will want to investigate these.

Pam
  • 1,146
  • 1
  • 14
  • 18
  • Thank you but I have already done it i need just certain folders to have this rule, others have different ones – Tomee May 04 '21 at 11:25
  • You might still do it with conditional string manipulation and os.path edits. – Pam May 04 '21 at 11:31
  • @Tomee this is a legitimate option. To restrict the conditional to specific folders simply check the path, if the path is the right folder, use the conditional Pam wrote. – ViaTech May 04 '21 at 12:49
  • Tried to figure it out, but i dont have clue @ViaTech Can you elaborate it please – Tomee May 04 '21 at 12:52