Renaming multiple files in a directory by names from text file using Python











up vote
-1
down vote

favorite












How can I rename all files in a folder with names from a text file?
Suppose I have a inventory.txt with the contents:



list-item1.ext
list-item2.ext


And there are files in a folder Downloads called:



file1.ext
file2.ext


So the new file names should be:



file1.ext    >>>  list-item1.ext
file2.ext >>> list-item2.ext


Thanks!



Here is updated code:



import os

path="c:\Users\dimai\Downloads"
files = os.listdir(path)
filename="c:/Users/dimai/Documents/inventory.txt"
print("Current File list:")
print(files)

with open(filename) as f:
mylist = f.read().splitlines()
print("Items from txt file:")
print(mylist)

str2 = ' '.join(mylist)

i = 1
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, str2))
i = i+1


Output:



Traceback (most recent call last):
File "C:UsersdimaiOneDriveBackupsPythonvideo-rename-0.2.py", line 18, in <module>
os.rename(os.path.join(path, file), os.path.join(path, str2))
FileExistsError: [WinError 183] Cannot create a file when that file already exists:: 'c:\Users\dimai\Downloads\file2.mp3' -> 'c:\Users\dimai\Downloads\list-item1.ext list-item2.ext'


So file1.ext was renamed to list-item1.ext list-item2.ext
and file2.ext was not renamed at all.










share|improve this question




















  • 1




    What have you tried so far?
    – Sharku
    Nov 9 at 13:08










  • Python os library has all the things you need here. Look into listdir and rename.
    – Milos Matovic
    Nov 9 at 13:37










  • Use readlines function - it gives you all the lines in the file in a list. Listdir also returns a list with actual file names. Check if the lists are same size then loop and rename. Don forget to append path to the file names as well
    – Milos Matovic
    Nov 9 at 14:07












  • I've tried to do that, but now I got syntax incorrect error. If there is only one file in folder - it will be renamed without any errors.
    – Deema Pozdin
    Nov 10 at 3:39

















up vote
-1
down vote

favorite












How can I rename all files in a folder with names from a text file?
Suppose I have a inventory.txt with the contents:



list-item1.ext
list-item2.ext


And there are files in a folder Downloads called:



file1.ext
file2.ext


So the new file names should be:



file1.ext    >>>  list-item1.ext
file2.ext >>> list-item2.ext


Thanks!



Here is updated code:



import os

path="c:\Users\dimai\Downloads"
files = os.listdir(path)
filename="c:/Users/dimai/Documents/inventory.txt"
print("Current File list:")
print(files)

with open(filename) as f:
mylist = f.read().splitlines()
print("Items from txt file:")
print(mylist)

str2 = ' '.join(mylist)

i = 1
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, str2))
i = i+1


Output:



Traceback (most recent call last):
File "C:UsersdimaiOneDriveBackupsPythonvideo-rename-0.2.py", line 18, in <module>
os.rename(os.path.join(path, file), os.path.join(path, str2))
FileExistsError: [WinError 183] Cannot create a file when that file already exists:: 'c:\Users\dimai\Downloads\file2.mp3' -> 'c:\Users\dimai\Downloads\list-item1.ext list-item2.ext'


So file1.ext was renamed to list-item1.ext list-item2.ext
and file2.ext was not renamed at all.










share|improve this question




















  • 1




    What have you tried so far?
    – Sharku
    Nov 9 at 13:08










  • Python os library has all the things you need here. Look into listdir and rename.
    – Milos Matovic
    Nov 9 at 13:37










  • Use readlines function - it gives you all the lines in the file in a list. Listdir also returns a list with actual file names. Check if the lists are same size then loop and rename. Don forget to append path to the file names as well
    – Milos Matovic
    Nov 9 at 14:07












  • I've tried to do that, but now I got syntax incorrect error. If there is only one file in folder - it will be renamed without any errors.
    – Deema Pozdin
    Nov 10 at 3:39















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











How can I rename all files in a folder with names from a text file?
Suppose I have a inventory.txt with the contents:



list-item1.ext
list-item2.ext


And there are files in a folder Downloads called:



file1.ext
file2.ext


So the new file names should be:



file1.ext    >>>  list-item1.ext
file2.ext >>> list-item2.ext


Thanks!



Here is updated code:



import os

path="c:\Users\dimai\Downloads"
files = os.listdir(path)
filename="c:/Users/dimai/Documents/inventory.txt"
print("Current File list:")
print(files)

with open(filename) as f:
mylist = f.read().splitlines()
print("Items from txt file:")
print(mylist)

str2 = ' '.join(mylist)

i = 1
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, str2))
i = i+1


Output:



Traceback (most recent call last):
File "C:UsersdimaiOneDriveBackupsPythonvideo-rename-0.2.py", line 18, in <module>
os.rename(os.path.join(path, file), os.path.join(path, str2))
FileExistsError: [WinError 183] Cannot create a file when that file already exists:: 'c:\Users\dimai\Downloads\file2.mp3' -> 'c:\Users\dimai\Downloads\list-item1.ext list-item2.ext'


So file1.ext was renamed to list-item1.ext list-item2.ext
and file2.ext was not renamed at all.










share|improve this question















How can I rename all files in a folder with names from a text file?
Suppose I have a inventory.txt with the contents:



list-item1.ext
list-item2.ext


And there are files in a folder Downloads called:



file1.ext
file2.ext


So the new file names should be:



file1.ext    >>>  list-item1.ext
file2.ext >>> list-item2.ext


Thanks!



Here is updated code:



import os

path="c:\Users\dimai\Downloads"
files = os.listdir(path)
filename="c:/Users/dimai/Documents/inventory.txt"
print("Current File list:")
print(files)

with open(filename) as f:
mylist = f.read().splitlines()
print("Items from txt file:")
print(mylist)

str2 = ' '.join(mylist)

i = 1
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, str2))
i = i+1


Output:



Traceback (most recent call last):
File "C:UsersdimaiOneDriveBackupsPythonvideo-rename-0.2.py", line 18, in <module>
os.rename(os.path.join(path, file), os.path.join(path, str2))
FileExistsError: [WinError 183] Cannot create a file when that file already exists:: 'c:\Users\dimai\Downloads\file2.mp3' -> 'c:\Users\dimai\Downloads\list-item1.ext list-item2.ext'


So file1.ext was renamed to list-item1.ext list-item2.ext
and file2.ext was not renamed at all.







python file-rename batch-rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:13

























asked Nov 9 at 13:04









Deema Pozdin

11




11








  • 1




    What have you tried so far?
    – Sharku
    Nov 9 at 13:08










  • Python os library has all the things you need here. Look into listdir and rename.
    – Milos Matovic
    Nov 9 at 13:37










  • Use readlines function - it gives you all the lines in the file in a list. Listdir also returns a list with actual file names. Check if the lists are same size then loop and rename. Don forget to append path to the file names as well
    – Milos Matovic
    Nov 9 at 14:07












  • I've tried to do that, but now I got syntax incorrect error. If there is only one file in folder - it will be renamed without any errors.
    – Deema Pozdin
    Nov 10 at 3:39
















  • 1




    What have you tried so far?
    – Sharku
    Nov 9 at 13:08










  • Python os library has all the things you need here. Look into listdir and rename.
    – Milos Matovic
    Nov 9 at 13:37










  • Use readlines function - it gives you all the lines in the file in a list. Listdir also returns a list with actual file names. Check if the lists are same size then loop and rename. Don forget to append path to the file names as well
    – Milos Matovic
    Nov 9 at 14:07












  • I've tried to do that, but now I got syntax incorrect error. If there is only one file in folder - it will be renamed without any errors.
    – Deema Pozdin
    Nov 10 at 3:39










1




1




What have you tried so far?
– Sharku
Nov 9 at 13:08




What have you tried so far?
– Sharku
Nov 9 at 13:08












Python os library has all the things you need here. Look into listdir and rename.
– Milos Matovic
Nov 9 at 13:37




Python os library has all the things you need here. Look into listdir and rename.
– Milos Matovic
Nov 9 at 13:37












Use readlines function - it gives you all the lines in the file in a list. Listdir also returns a list with actual file names. Check if the lists are same size then loop and rename. Don forget to append path to the file names as well
– Milos Matovic
Nov 9 at 14:07






Use readlines function - it gives you all the lines in the file in a list. Listdir also returns a list with actual file names. Check if the lists are same size then loop and rename. Don forget to append path to the file names as well
– Milos Matovic
Nov 9 at 14:07














I've tried to do that, but now I got syntax incorrect error. If there is only one file in folder - it will be renamed without any errors.
– Deema Pozdin
Nov 10 at 3:39






I've tried to do that, but now I got syntax incorrect error. If there is only one file in folder - it will be renamed without any errors.
– Deema Pozdin
Nov 10 at 3:39



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226254%2frenaming-multiple-files-in-a-directory-by-names-from-text-file-using-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226254%2frenaming-multiple-files-in-a-directory-by-names-from-text-file-using-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud