简体   繁体   中英

placing file in folder with number in python

I am just beginning with python and I'm trying to place a file inside a directory. The directory has a name with a number behind it that changes depending on the creation date. The function I'm using for placing the file takes only the directory + the file as an argument. So an argument for the function could be: C:\\Program Files\\example241\\file.txt , C:\\Program Files\\example948\\file.txt etc. How would I place the file in that folder no mather what number it has? Could I use a wildcard?

you can use the glob module

from glob import glob
# this gives all the folders that answer to this pattern 
folders = glob(r'C:\Program Files\example*')  
folder = folders[0]  # pick the first one

append the desired file name with the os module

import os
file_name = 'file.txt'
file_path = os.path.join(folder, file_name)

and then save the file to file_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM