Jump to content

Anyone know python?

Go to solution Solved by WereCatf,

Well, looks like I'm getting no reply, so I'm just leaving this here:

#!/usr/bin/python3

from os import listdir, rename, chdir
from os.path import isfile, join

#Replace the path with whatever you like
#Or write the code to get it from the command-line
dirPath = "."
chdir(dirPath)

#Find only files, not directories
listFiles = [f for f in listdir(dirPath) if isfile(join(dirPath, f))]
for filename in listFiles:
	countHashes = filename.count("#")
	if(countHashes < 2):
		continue
	#Remove the 2nd .replace() here, if you just want to ignore the hash
	#Or change the space to whatever you want to replace the last hash into
	newFilename = filename.replace("#", "_", countHashes - 1).replace("#", " ")
	if(newFilename != filename):
		#Remove the 2nd .replace() here, if you just want to ignore the hash
		#Or change the space to whatever you want to replace the last hash into
		print("Rename {:s} to {:s}".format(filename, filename.replace("#", "_", countHashes - 1).replace("#", " ") ))
		if(isfile(newFilename) == False):
			rename(filename, newFilename)
		else:
			print("File already exists, not renaming.")

 

I need a application to rename all files in a directory from a hashtag to a dash. "#" to "-" but have no idea how. Can someone help?

 

e.g. 

internalstats#2015#2016

to 

internalstats-2015-2016

 

The reason I need to do this through the use of an application is because there are about 300 files.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/
Share on other sites

Link to post
Share on other sites

Why don't you just use one of the already-available tools for such? Under Linux "mmv" is a really good tool for this and for Windows there are quite a few. I think "Magic File Renamer" would suffice, though, I have never used it myself.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11534555
Share on other sites

Link to post
Share on other sites

2 minutes ago, WereCatf said:

Why don't you just use one of the already-available tools for such? Under Linux "mmv" is a really good tool for this and for Windows there are quite a few. I think "Magic File Renamer" would suffice, though, I have never used it myself.

Because I wanna add it into another application that I've written for efficiency.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11534558
Share on other sites

Link to post
Share on other sites

28 minutes ago, mattonfire said:

Because I wanna add it into another application that I've written for efficiency.

Well, okay then. Here's a simplistic and a tad naive example, but it should get you going:

#!/usr/bin/python3

from os import listdir, rename, chdir
from os.path import isfile, join

#Replace the path with whatever you like
#Or write the code to get it from the command-line
dirPath = "."
chdir(dirPath)

#Find only files, not directories
listFiles = [f for f in listdir(dirPath) if isfile(join(dirPath, f))]
for filename in listFiles:
	newFilename = filename.replace("#", "_")
	if(newFilename != filename):
		print("Rename {:s} to {:s}".format(filename, filename.replace("#", "_")))
		if(isfile(newFilename) == False):
			rename(filename, newFilename)
		else:
			print("File already exists, not renaming.")

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11534609
Share on other sites

Link to post
Share on other sites

1 hour ago, WereCatf said:

Well, okay then. Here's a simplistic and a tad naive example, but it should get you going:


#!/usr/bin/python3

from os import listdir, rename, chdir
from os.path import isfile, join

#Replace the path with whatever you like
#Or write the code to get it from the command-line
dirPath = "."
chdir(dirPath)

#Find only files, not directories
listFiles = [f for f in listdir(dirPath) if isfile(join(dirPath, f))]
for filename in listFiles:
	newFilename = filename.replace("#", "_")
	if(newFilename != filename):
		print("Rename {:s} to {:s}".format(filename, filename.replace("#", "_")))
		if(isfile(newFilename) == False):
			rename(filename, newFilename)
		else:
			print("File already exists, not renaming.")

 

Works perfectly, but sorry forgot something. Is there a way to ignore the last # in the file name. Like imported#data#2016#2017 for example  imported_data_2016 2017

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11534810
Share on other sites

Link to post
Share on other sites

5 minutes ago, mattonfire said:

Works perfectly, but sorry forgot something. Is there a way to ignore the last # in the file name. Like imported#data#2016#2017 for example  imported_data_2016 2017

But you're not ignoring it in that example, you're changing it to a space? Be specific now, do you want to ignore it or change it to space?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11534833
Share on other sites

Link to post
Share on other sites

Well, looks like I'm getting no reply, so I'm just leaving this here:

#!/usr/bin/python3

from os import listdir, rename, chdir
from os.path import isfile, join

#Replace the path with whatever you like
#Or write the code to get it from the command-line
dirPath = "."
chdir(dirPath)

#Find only files, not directories
listFiles = [f for f in listdir(dirPath) if isfile(join(dirPath, f))]
for filename in listFiles:
	countHashes = filename.count("#")
	if(countHashes < 2):
		continue
	#Remove the 2nd .replace() here, if you just want to ignore the hash
	#Or change the space to whatever you want to replace the last hash into
	newFilename = filename.replace("#", "_", countHashes - 1).replace("#", " ")
	if(newFilename != filename):
		#Remove the 2nd .replace() here, if you just want to ignore the hash
		#Or change the space to whatever you want to replace the last hash into
		print("Rename {:s} to {:s}".format(filename, filename.replace("#", "_", countHashes - 1).replace("#", " ") ))
		if(isfile(newFilename) == False):
			rename(filename, newFilename)
		else:
			print("File already exists, not renaming.")

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11534980
Share on other sites

Link to post
Share on other sites

@WereCatf
 You should just check if the file name contains  a #, your code current has to create the file name for every single file but that action is only needed for renaming.

 

Might not matter here but it's worth keeping in mind if you want things to be performant on large data sets.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11535002
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

@WereCatf
 You should just check if the file name contains  a #, your code current has to create the file name for every single file but that action is only needed for renaming.

 

Might not matter here but it's worth keeping in mind if you want things to be performant on large data sets.

Aye, like I said, it was just some simple and naive code that I quickly threw together. Didn't bother to stop thinking about optimizing it, I'll leave that as an exercise to the OP.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11535008
Share on other sites

Link to post
Share on other sites

1 hour ago, WereCatf said:

But you're not ignoring it in that example, you're changing it to a space? Be specific now, do you want to ignore it or change it to space?

Sorry for jumping, I'm fucking terrible at multitasking and kinda' stressed. But you absolutely solved my problem thank you kind Sir.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11535135
Share on other sites

Link to post
Share on other sites

42 minutes ago, vorticalbox said:

@WereCatf
 You should just check if the file name contains  a #, your code current has to create the file name for every single file but that action is only needed for renaming.

 

Might not matter here but it's worth keeping in mind if you want things to be performant on large data sets.

It's perfect for what I need, not a big issue at all.

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11535137
Share on other sites

Link to post
Share on other sites

11 hours ago, WereCatf said:

Aye, like I said, it was just some simple and naive code that I quickly threw together. Didn't bother to stop thinking about optimizing it, I'll leave that as an exercise to the OP.

That's fair always good to grtpeople to learn :)

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/947730-anyone-know-python/#findComment-11536463
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×