Skip to content

Instantly share code, notes, and snippets.

@johnjohndoe
Last active July 13, 2022 16:47
Show Gist options
  • Save johnjohndoe/b2a067c0557dd1122b16b09650498adb to your computer and use it in GitHub Desktop.
Save johnjohndoe/b2a067c0557dd1122b16b09650498adb to your computer and use it in GitHub Desktop.
Shuffle two file names and copy the active file to a destination folder.
#!/bin/python3
import os
import sys
from shutil import copyfile
def getScriptPath():
"""
Returns the full directory path of this script.
"""
return sys.path[0]
FILE_ACTIVE = "{0}/file.xml".format(getScriptPath())
FILE1_INACTIVE = "{0}/file1.xml".format(getScriptPath())
FILE2_INACTIVE = "{0}/file2.xml".format(getScriptPath())
FILE_PATH_ACTIVE = "/tmp/file.xml"
class FileNotFoundException(Exception):
pass
def shuffleFileNames():
"""
Shuffles the names of two files.
Initially there must be two files
matching FILE_ACTIVE and
FILE1_INACTIVE or FILE2_INACTIVE.
"""
if os.path.isfile(FILE_ACTIVE) and os.path.isfile(FILE2_INACTIVE):
print(f"Renaming {FILE_ACTIVE} -> {FILE1_INACTIVE}")
os.rename(FILE_ACTIVE, FILE1_INACTIVE)
print(f"Renaming {FILE2_INACTIVE} -> {FILE_ACTIVE}")
os.rename(FILE2_INACTIVE, FILE_ACTIVE)
elif os.path.isfile(FILE_ACTIVE) and os.path.isfile(FILE1_INACTIVE):
print(f"Renaming {FILE_ACTIVE} -> {FILE2_INACTIVE}")
os.rename(FILE_ACTIVE, FILE2_INACTIVE)
print(f"Renaming {FILE1_INACTIVE} -> {FILE_ACTIVE}")
os.rename(FILE1_INACTIVE, FILE_ACTIVE)
else:
raise "File(s) not found."
def copyFile():
"""
Copies a file to a destination folder.
"""
if not os.path.isfile(FILE_ACTIVE):
raise FileNotFoundException(f"File {FILE_ACTIVE} not found.")
if not os.path.isfile(FILE_PATH_ACTIVE):
raise FileNotFoundException(f"File path {FILE_PATH_ACTIVE} not found.")
print(f"Coping {FILE_ACTIVE} -> {FILE_PATH_ACTIVE}")
copyfile(FILE_ACTIVE, FILE_PATH_ACTIVE)
shuffleFileNames()
copyFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment