Avatar ·

Generating random string

📁 Advanced Python Topics, Help

I'm interested in how to generate a string like sa32Asf7w1 using the random library in Python. The first 4 characters are a random set of letters and digits, the same for characters 6 through 10. The 5th character is a random uppercase letter.

Avatar ·

Let there be some path:

path = r'C:\\Python27'

Need to get a list of files at this path:

import osfiles = os.listdir(path)

At this stage, it's advisable to check that the list of files is not empty. We simply do if files:, and everything described below goes inside the if block.

Let's turn a simple list of files into a list of files with paths:

files = [os.path.join(path, file) for file in files]

The os.path.join() function "attaches" the path to the file name. Depending on the system, this function inserts between

Log in to leave an answer

Blogs