-1

I'm looking to copy the files from source directory to the remote directory using shutil(), however I need to have few checks as follows.

  1. Don't copy the zero byte file to the remote.

  2. If the file is already exits on the remote then don't copy it again unless the file in source has changed contents or updated.

  3. I'm looking for the directory which is of current month, so, traverse to the directory if its available for the current month, like it should be January for the current month.

Importing the modules:

import os
import glob
import shutil
import datetime

Variable to pick the current month:

Info_month = datetime.datetime.now().strftime("%B")

Code snippet:

for filename in glob.glob("/data/Info_month/*/*.txt"):
    if not os.path.exists("/remote/data/" + os.path.basename(filename)):
        shutil.copy(filename, "/remote/data/")

Above code doesn't take the variable Info_month However, hardcoding the directory name works.

I'm having challenges due to my lack of Python knowledge.

How can I include the variable Info_month into the source dir path?

How to place the check for not to copy zero byte files?

os.path.getsize(fullpathhere) > 0

My rudimentary silly logic:

for filename in glob.glob("/data/Info_month/*/*.txt"):
    if os.path.getsize(fullpathhere) > 0 :
        if not os.path.exists("/remote/data/" + os.path.basename(filename)):
            shutil.copy(filename, "/remote/data/")
    else:
        pass
krock1516
  • 441
  • 10
  • 30
  • Not my downvote, but you seem to be combining a lot of very basic questions into one post. A good way to get an answer on Stack Overflow is to ask *exactly one* question, and demonstrate exactly where you are stuck after googling for answers – tripleee Jan 09 '19 at 06:57
  • For example, "How do I interpolate a variable into a Python string" gets me https://stackoverflow.com/questions/3542714/variable-interpolation-in-python as the first Google result. – tripleee Jan 09 '19 at 06:57
  • Using month *names* as directory names seems like a poor idea. Use the month number to make them easily computer-accessible and also sort properly when you list them. – tripleee Jan 09 '19 at 06:59
  • @tripleee, thanks a lot for the explanation, Indeed I need to use month `names` here due to some reason. – krock1516 Jan 09 '19 at 07:53

1 Answers1

1

Here is a fix of your existing script. This doesn't yet attempt to implement the "source newer than target" logic since you didn't specifically ask about that, and this is arguably too broad already.

for filename in glob.glob("/data/{0}/*/*.txt".format(Info_month)):
    # The result of the above glob _is_ a full path
    if os.path.getsize(filename) > 0:
        # Minor tweak: use os.path.join for portability
        if not os.path.exists(os.path.join(["/remote/data/", os.path.basename(filename)])):
            shutil.copy(filename, "/remote/data/")
    # no need for an explicit "else" if it's a no-op
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks again for the fix, i'll raise another question for the for the data check `source newer than target` in terms of file contents. – krock1516 Jan 09 '19 at 07:57
  • Quick googling gets me https://stackoverflow.com/questions/14779775/how-to-compare-the-modified-date-of-two-files-in-python – tripleee Jan 09 '19 at 07:59
  • @ tripleee .. thanks for this & your expertise i'll try to get googling in case I fails to understand i'll raise a new question, hope that will be expectable. However i'm looking for the data on the both side files if that's similar then not to overwrite and only write the newer bits from source file. – krock1516 Jan 09 '19 at 08:01