Reputation: 33
I'm trying to create a sequence of folders which contains sub-folders which contains files with python.
The name of these folders come from a text file which has a list with the paths inside, like this:
songs\101_TNTOGCD\101_2020_CONTENT_LOOP_TNTOGCD.mov
songs\101_TNTOGCD\101_TNTOGCD_CONTENT_TC_A+B_CBLDESIGN2.mov
The number of the sub-folders it's not always the same, sometimes it can be two sub-folders and file, sometimes just one folder and file.
All of this must be created in a specified folder of my choice.
This is what I did so far:
import os
import shutil
import os.path
with open("C:\\Users\\Desktop\\folder1\\folder2\\folder3\\textfile.txt", "r") as g:
lines = g.readlines()
base_path = "C:\\Users\\Desktop\\folder1\\folder2\\folder3"
mov_origin = "C:\\Users\\Desktop\\folder1\\folder2\\folder3\\folder4\\file_000.mov"
for line in lines:
target = base_path
path = line.split("\\")
for i in range(len(path) - 1):
target += path[i]
if not os.path.exists(target):
os.makedirs(target)
else:
pass
target += "\\"
target += path[-1].replace("\n", "")
print(target)
shutil.copyfile(mov_origin, target)
But basically, when I run it, nothing happens. I didn't get any error, but nothing is created. What I'm doing wrong?
Thanks in advance for all your help!
UPDATE: The code was not working because of my mistake in the Run/Debug configuration in PyCharm. The wrong Script Path was selected.
Upvotes: 2
Views: 281
Reputation: 33
The code was working. The issue happened because of my mistake in the Run/Debug configuration in PyCharm. The wrong Script Path was selected.
Upvotes: 1
Reputation: 909
I think you need to give it something to open. The line
with open("C:\\Users\\xxxx\\xxxx\\xxxx\\xxxx\\xxxx.txt", "r") as g:
cannot do anything unless you have folders actually called XXXX... etc. What does it do when you have a proper file path in there?
When I tried this it printed the lines that are within that text file. I would liberally use print statements until you see where the issue is.
import os
import shutil
import os.path
#lines = []
with open("/Users/.../Desktop/OPW_DATA/comments.txt", "r") as g:
lines = g.readlines()
base_path = "/Users/.../Desktop/"
#mov_origin = "C:\\Users\\xxxx\\xxxx\\xxxx\\xxxx\\xxxx\\file_000.mov"
for line in lines:
print('line:', line)
target = base_path
print('target:',target)
path = line.split("\\")
for i in range(len(path) - 1):
target += path[i]
if not os.path.exists(target):
os.makedirs(target)
else:
pass
target += "\\"
target += path[-1].replace("\n", "")
print(target)
#shutil.copyfile(mov_origin, target)
Upvotes: 0