Answer the question
In order to leave comments, you need to log in
How to make sure that when moving files they are not replaced?
Recently on YouTube I caught a glimpse of the idea of how people were making a sorter in a folder.
I tried to do it myself (I'm just learning python), as a result, 2 problems arose.
I needed to move a file from the D:\Downloads directory to the D:\Downloads\Text directory (for example)
I tried to use the function from os replace
if os.path.splitext(file)[1] == '.txt':
os.replace(file, "D:\Downloads\Text")
import os
# import calendar
os.chdir(r"D:\Downloads")
dirs = ['Изображения', 'Видео', 'Торрент', 'Установщики', 'Zip', 'Аудио(Музыка)', 'Остальное', 'Text']
for name in dirs:
if os.path.isdir(name):
continue
os.mkdir(r"{}".format(name))
swaps = True
while True:
swaps = False
for file in os.listdir():
if os.path.isfile(file):
if os.path.splitext(file)[1] == '.txt':
os.replace(file, "D:\Downloads\Text")
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question