0

I Want a script that renames a file on my computer to something random every second, I have the code to rename it here

import os
os.rename('Original-File-Name', 'New-File-Name')

Here's the script that outputs a random letter

import string
import random
string.ascii_letters = "abcdefghijklmnopqrstuvwxyz1234567890"
random.choice(string.ascii_letters)

I Want it to rename the file with a random letter every second, how would I do that? (I'm not very good with loops)

Daniel
  • 42,087
  • 4
  • 55
  • 81
  • Possible duplicate of [What is the best way to repeatedly execute a function every x seconds in Python?](https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python) – Ludovit Mydla Dec 26 '18 at 03:14

2 Answers2

0

Try to add infinite loop:

import time

old_file_name = 'file name here'
while True:
    time.sleep(1)
    new_file_name = generate_random_name()
    rename_file(old_file_name, new_file_name)
    old_file_name = new_file_name

Also you should save new random file name, to rename it in next second

nick nick
  • 129
  • 3
  • **How would I change the name to something random?** `import os` import random import string import time time.sleep(1) String = string.ascii_letters = "abcdefghijklmnopqrstuvwxyz1234567890" `Random = random.choice(String)` `while True:` ` time.sleep(1)` `os.rename('Original-File-Name', Random);` – ItsYoBoiExo123 Dec 16 '18 at 20:19
  • 1. generate something random, 2. change name – nick nick Dec 16 '18 at 20:26
  • Since the name has changed I need to change the "Original-File-Name" With the new one – ItsYoBoiExo123 Dec 16 '18 at 20:30
  • Traceback (most recent call last): File "C:\Users\*****\Desktop\ModuleTest.py", line 6, in new_file_name = generate_random_name() NameError: name 'generate_random_name' is not defined – ItsYoBoiExo123 Dec 16 '18 at 20:41
0

try this

import os 
import random 
import string 
import time 
String = string.ascii_letters = "abcdefghijklmnopqrstuvwxyz1234567890" 
Random = random.choice(String) 
while True: 
    time.sleep(1) 
    name = str(time.time()).split('.')
    os.rename('Original-File-Name', Random+name[0]+'_'+name[1]);
sahasrara62
  • 10,069
  • 3
  • 29
  • 44