So I am fairly new to Python and I have scoured the web and stack overflow for a simple solution. I am sure there is a solution I have come across that would work. However I am having a hard time implementing other peoples solutions into the context of what I need.
I have a template file that I have managed to copy into multiple sub directories of my “projStorage” variable.
e.g
+ proj\Storage\dir
+ Seq1
+ template.file >> which I want to rename to >> Seq1_stringHere.File
+ Seq2
+ template.file >> Seq2_stringHere.File
+ Seq3
+ template.file >> Seq3_stringHere.File
The problem is that I want to rename my template files based on the directory they have just been copied to. To do this I was doing an os.listdir(seqFileList) which I was hoping would return a list with each template file so I could do a rename to the Seq Directory name + a string format.
I have done this using a for loop. Now I understand that my ‘seqFileList’ is doing a listdir() for each folder in the proj\Storage\Dir and is returning my list of files.
I have seen solutions of ‘flattening a list’ but I am not quite understanding how that works.
Printing seqFileList returns something like,
['Template.file']
['Template.file']
['Template.file']
when what I want is
['Template.file','Template.file','Template.file',]
so I can rename to
['Seq01_strHere.File','Seq02_strHere.File','Seq03_strHere.File',]
Also I am aware that the rename part of the code is not implemented yet as I am unsure of how to go about that. I know one solution is the os.rename() however I am not sure of where to properly implement this.
I understand that this question is probably not new and will be remarkably simple for some people. I am not trying to be a “Help Vampire” as I am really trying to internalize this stuff for myself. So please forgive my newness.
Any and all solutions are appreciated.
I have replaced file paths and project names for clarity.
Thanks in advance.
import os, shutil
projTemplate = 'Path\\To\\Template.file'
projStorage = 'Proj\\Storage\\dir'
projSeqs = os.listdir(projStorage)
for seqs in projSeqs:
seqFileDir = os.path.join(projStorage, seqs)
shutil.copy2(projTemplate, seqFileDir)
seqFileList = os.listdir(seqFileDir)
print seqFileDir
Why not rename it at the point you’re copying the template in your subfolders?