Quantcast
Channel: How to rename a file using Python - Stack Overflow
Browsing latest articles
Browse All 18 View Live

Answer by Elvin Aghammadzada for How to rename a file using Python

How to change the first letter of filename in a directory:import ospath = "/"for file in os.listdir(path): os.rename(path + file, path + file.lower().capitalize())then = os.listdir(path)print(then)

View Article



Answer by gil.fernandes for How to rename a file using Python

Here is an example using pathlib only without touching os which changes the names of all files in a directory, based on a string replace operation without using also string concatenation:from pathlib...

View Article

Answer by Chris Collett for How to rename a file using Python

As of Python version 3.3 and later, it is generally preferred to use os.replace instead of os.rename so FileExistsError is not raised if the destination file already exists.assert...

View Article

Answer by Lohith for How to rename a file using Python

One important point to note here, we should check if any files exists with the new filename.suppose if b.kml file exists then renaming other file with the same filename leads to deletion of existing...

View Article

Answer by sauravjoshi23 for How to rename a file using Python

os.chdir(r"D:\Folder1\Folder2")os.rename(src,dst) #src and dst should be inside Folder2

View Article


Answer by kym for How to rename a file using Python

Using the Pathlib library's Path.rename instead of os.rename:import pathliboriginal_path = pathlib.Path('a.txt')new_path = original_path.rename('b.kml')

View Article

Answer by Khan Saad for How to rename a file using Python

If you are Using Windows and you want to rename your 1000s of files in a folder then:You can use the below code. (Python3)import ospath = os.chdir(input("Enter the path of the Your Image Folder : "))...

View Article

Answer by issam for How to rename a file using Python

import osimport refrom pathlib import Pathfor f in os.listdir(training_data_dir2): for file in os.listdir( training_data_dir2 +'/'+ f): oldfile= Path(training_data_dir2 +'/'+ f +'/'+ file) newfile =...

View Article


Answer by Tilak M Divakar for How to rename a file using Python

import os# Set the pathpath = 'a\\b\\c'# save current working directorysaved_cwd = os.getcwd()# change your cwd to the directory which contains filesos.chdir(path)os.rename('a.txt', 'b.klm')# moving...

View Article


Answer by lonewolf for How to rename a file using Python

Use os.rename. But you have to pass full path of both files to the function. If I have a file a.txt on my desktop so I will do and also I have to give full of renamed file...

View Article

Answer by Naveen for How to rename a file using Python

import shutilimport osfiles = os.listdir("./pics/") for key in range(0, len(files)): print files[key] shutil.move("./pics/"+ files[key],"./pics/img"+ str(key) +".jpeg")This should do it. python 3+

View Article

Answer by idjaw for How to rename a file using Python

As of Python 3.4 one can use the pathlib module to solve this.If you happen to be on an older version, you can use the backported version found hereLet's assume you are not in the root path (just to...

View Article

Answer by user5449023 for How to rename a file using Python

You can use os.system to invoke terminal to accomplish the task:os.system('mv oldfile newfile')

View Article


Answer by Abdul Razak for How to rename a file using Python

File may be inside a directory, in that case specify the path:import osold_file = os.path.join("directory", "a.txt")new_file = os.path.join("directory", "b.kml")os.rename(old_file, new_file)

View Article

Answer by Joe for How to rename a file using Python

os.rename(old, new)This is found in the Python docs: http://docs.python.org/library/os.html

View Article


Answer by YOU for How to rename a file using Python

Use os.rename:import osos.rename('a.txt', 'b.kml')Usage:os.rename('from.extension.whatever','to.another.extension')

View Article

Answer by Andy Balaam for How to rename a file using Python

import shutilshutil.move('a.txt', 'b.kml')This will work to rename or move a file.

View Article


How to rename a file using Python

I want to change a.txt to b.kml.

View Article
Browsing latest articles
Browse All 18 View Live




Latest Images