Hack Forums
[Python] Quick python script to change all file names of an extension - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Quick python script to change all file names of an extension (/showthread.php?tid=5702647)

Pages: Pages 1 2


Quick python script to change all file names of an extension - Lukos1234 - Aug 24, 2017

import os
import random

os.getcwd()


extension = input("What's the extension of the file names you want to change ? \nexample : .mp4 or .jpg \n")
for f in os.listdir():
fname , fext = os.path.splitext(f)
if fext==extension:
fname = str(random.randint(1,100000000))
newname= "{}{}".format(fname,fext)
os.rename(f,newname)

just learning guys 😁


* - Vana - Aug 24, 2017

Use "php" or "code" tags to preserve indentation when posting code.
PHP Code

<?php 
import os
import random

os
.getcwd()

extension = input("What's the extension of the file names you want to change ? \nexample : .mp4 or .jpg \n")
for 
f in os.listdir():
    
fname , fext = os.path.splitext(f)
    if 
fext==extension:
        
fname = str(random.randint(1,100000000))
        
newname= "{}{}".format(fname,fext)
        
os.rename(f,newname) 



* - Lukos1234 - Aug 24, 2017

Yea, actually forgot them but learned them from you in the first place , thanks


* - vorticalbox - Aug 25, 2017

Because sod readability
PHP Code

<?php 
#!/urs/bin/python3
import os
import random

extension 
= input("What's the extension of the file names you want to change ? \nexample : .mp4 or .jpg \n")
[
os.rename(f,str(random.randint(1,10**6))+os.path.splitext(f)[1]) for f in os.listdir() if os.path.splitext(f)[1] == extension] 



* - Ensky - Aug 25, 2017

You guys know he's trying to create some form of ransomware, right?


* - vorticalbox - Aug 25, 2017

(Aug 25, 2017 06:45 AM)The KGB Wrote:  You guys know he's trying to create some form of ransomware, right?

I heard renaming files or removing their extension is a good ransomware.

Either way the script would be open for anyone to see.


* - Lukos1234 - Aug 25, 2017

(Aug 25, 2017 06:45 AM)The KGB Wrote:  You guys know he's trying to create some form of ransomware, right?

Wuuuuut ? I'm a beginner man , didn't even think of it , but now that you say it 😈 😈 😁


* - emento - Aug 26, 2017

(Aug 25, 2017 06:04 AM)vorticalbox Wrote:  Because sod readability
PHP Code

<?php 
#!/urs/bin/python3
import os
import random

extension 
= input("What's the extension of the file names you want to change ? \nexample : .mp4 or .jpg \n")
[
os.rename(f,str(random.randint(1,10**6))+os.path.splitext(f)[1]) for f in os.listdir() if os.path.splitext(f)[1] == extension] 

This isn't good practice. The beauty of Python is in its readability, one liners are nice and all, but this isn't a good way to do things.


* - vorticalbox - Aug 26, 2017

(Aug 26, 2017 03:42 AM)emento Wrote:  This isn't good practice. The beauty of Python is in its readability, one liners are nice and all, but this isn't a good way to do things.

I know I just let trying to one liner things


* - nOrr - Aug 26, 2017

(Aug 25, 2017 06:04 AM)vorticalbox Wrote:  Because sod readability
PHP Code

<?php 
#!/urs/bin/python3
import os
import random

extension 
= input("What's the extension of the file names you want to change ? \nexample : .mp4 or .jpg \n")
[
os.rename(f,str(random.randint(1,10**6))+os.path.splitext(f)[1]) for f in os.listdir() if os.path.splitext(f)[1] == extension] 

I just had involuntary nervous twitches.


* - vorticalbox - Aug 26, 2017

(Aug 26, 2017 05:08 AM)nOrr Wrote:  I just had involuntary nervous twitches.

Next time I'll add a trigger warning :P


* - nOrr - Aug 26, 2017

(Aug 26, 2017 05:51 AM)vorticalbox Wrote:  Next time I'll add a trigger warning :P

🤝

[warn]


* - zer0dev - Aug 27, 2017

@Lukos123:

If you are interested in renaming files to random strings instead of numbers, you may use module 'strgen'


To generate 12 letter random string:

Code
import strgen
strgen.StringGenerator('[\1]{12}').render()



* - NoAimNoGame - Aug 28, 2017

Nice Code man.

PHP Code

<?php 
import os
Kys 
= input("What do you want to do? : ")
if 
Kys == 'kys':
       
os.system('KYS!')
      print(
'No such command but you killed yourself anyway') 



* - Lukos1234 - Aug 30, 2017

(Aug 27, 2017 11:56 AM)zer0dev Wrote:  @Lukos123:

If you are interested in renaming files to random strings instead of numbers, you may use module 'strgen'


To generate 12 letter random string:

Code
import strgen
strgen.StringGenerator('[\1]{12}').render()

thanks a lot 🤝