1

I have a little task for my company

I have multiple files which start with swale-randomnumber

I want to copy then to some directory (does shutil.copy allow wildmasks?)

anyway I then want to choose the largest file and rename it to sync.dat and then run a program.

I get the logic, I will use a loop to do each individual piece of work then move on to the next, but I am unsure how to choose a single largest file or a single file at all for that matter as when I type in swale* surely it will just choose them all?

Sorry I havnt written any source code yet, I am still trying to get my head around how this will work.

Thanks for any help you may provide

hexce
  • 151
  • 1
  • 1
  • 12
  • 1
    Stack overflow isn't a code development service - but I will give you some hints : shutil doesn't support wildcards as far as I know. Wildcard expansion is done by the command line shell. You will need to use the glob module to do your wildcard matches and identify the files you need to copy before you do copies. you will need to use os.path.size to pick the biggest file. – Tony Suffolk 66 Aug 04 '14 at 08:36

3 Answers3

1

The accepted answer of this question proposes a nice portable implementation of file copy with wildcard support:

from glob import iglob
from shutil import copy
from os.path import join

def copy_files(src_glob, dst_folder):
    for fname in iglob(src_glob):
        copy(fname, join(dst_folder, fname))

If you want to compare file sizes, you can use either of these functions:

import os
os.path.getsize(path)
os.stat(path).st_size 
Community
  • 1
  • 1
Vincent
  • 12,919
  • 1
  • 42
  • 64
0

This might work :

import os.path
import glob
import shutil

source = "My Source Path" # Replace these variables with the appropriate data
dest = "My Dest Path"
command = "My command"

# Find the files that need to be copied
files = glob.glob(os.path.join(source, "swale-*"))

# Copy the files to the destination
for file in files:
     shutil.copy(os.path.join(source, "swale-*"), dest)

# Create a sorted list of files - using the file sizes
# biggest first, and then use the 1st item 
biggest = sorted([file for file in files], 
        cmp=lambda x,y : cmp(x,y), 
        key=lambda x: os.path.size( os.path.join( dest, x)),  reverse = True)[0]

# Rename that biggest file to swale.dat
shutil.move( os.path.join(dest,biggest), os.path.join(dest,"swale.date") )

# Run the command 
os.system( command ) 
# Only use os.system if you know your command is completely secure and you don't need the output. Use the popen module if you need more security and need the output.

Note : None of this is tested - but it should work

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
0
from os import *
from os.path import *

directory = '/your/directory/'

# You now have list of files in directory that starts with "swale-"
fileList = [join(directory,f) for f in listdir(directory) if f.startswith("swale-") and isfile(join(directory,f))]

# Order it by file size - from big to small
fileList.sort(key=getsize, reverse=True)

# First file in array is biggest
biggestFile = fileList[0]

# Do whatever you want with this files - using shutil.*, os.*, or anything else..
# ...
# ...
rufanov
  • 3,266
  • 1
  • 23
  • 41