Skip to content

Instantly share code, notes, and snippets.

@Robokishan
Created March 2, 2020 08:21
Show Gist options
  • Save Robokishan/c0b36db978b14b4eafcef6870ddd182c to your computer and use it in GitHub Desktop.
Save Robokishan/c0b36db978b14b4eafcef6870ddd182c to your computer and use it in GitHub Desktop.
rename files according to its symlink
import os
import argparse
parser = argparse.ArgumentParser(description='Rename files to its symlink')
parser.add_argument('--dir',default=None, type=str)
parser.add_argument('--file', default=None ,type=str)
parser.add_argument('--files', default=None ,nargs='*')
args = parser.parse_args()
runForDir = False
runForFile = False
runForMultipleFiles = False
filePath=None
folderPath=None
multiplefiles=None
print (args.dir)
print (args.file)
print (args.files)
if args.dir != None:
runForDir=True
if args.file != None:
runForFile=True
if args.files != None:
runForMultipleFiles = True
if runForDir == True:
folderPath=args.dir
files = os.listdir(folderPath)
for index, filename in enumerate(files):
if(os.path.islink(file) == True):
symlink = filename
original = os.path.realpath(filename)
os.unlink(symlink)
os.rename(original,symlink)
print(os.path.realpath(symlink))
if runForMultipleFiles == True:
multiplefiles = args.files
for index, filename in enumerate(multiplefiles):
if(os.path.islink(filename) == True):
symlink = filename
original = os.path.realpath(symlink)
print ("Symlink:{}".format(symlink))
print ("Original:{}".format(os.path.realpath(original)))
os.unlink(symlink)
os.rename(original,symlink)
print(os.path.realpath(symlink))
if runForFile == True:
filename=args.file
if (os.path.islink(filename)):
symlink = filename
original = os.path.realpath(filename)
os.unlink(symlink)
os.rename(original,symlink)
print(os.path.realpath(symlink))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment