LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 01-05-2021, 08:12 AM   #1
rincon
LQ Newbie
 
Registered: Mar 2020
Posts: 18

Rep: Reputation: Disabled
renaming a file using Python with a little one-liner.


hello guys good day


first of all: Happy New Year to everybody! I hope this year will not be a Covid year.


i want to change foo.txt to bar.txt

in other words renaming a file using Python

want to change
Code:
foo.txt to bar.txt

generally spoken - we could make use of this little one-liner:

Code:
import os
os.rename(r'file path\the old [former] name of the file name.file type',r'file path\the new file name.file type')
that means:
Code:
import os
os.rename(r'C:\Users\butch_miller\the_butch_desktop\Test\foo.txt',r'C:\Users\butch_miller\the_butch_desktop\Test\bar.txt')

by the way:
on a sidenote i have another question: Is this always necessary to use the character "r" before the so called path declaration in this python one-liner?

Well, I was wondering why would I need to make use of r before the path in our little python one-liner? Guess that this is becaue of the'\' in the path?


Is there any other way to give path instead of using r''

Well in other words: do i need the syntax like this in python code:

Code:
  import os
    os.chdir(r'C:\Users\butch_miller\the_butch_desktop')
some musings have led to the following thoughts:

isnt it like so:
- Windows-os allways does understand both of the above mentioned syntaxes of our one-liner: this expression "" and that expression "/"
- that said it would be possible to run the line whilst using "/" : would this be possible?!
i guess that i now would be able to avoid 'r' letter before the rest of the path string.



hmmm - is there any shorter way to do the job of renaming a file using Python with a little one-liner? _I guess that the various versions of Python give us many many options so that we probably could write a shorter code.


And probably a renaming of a bunch of files would not be too complicated too? i will digg deeper and come back if i have found out and tested more options.


have a great time
 
Old 01-05-2021, 08:30 AM   #2
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 1,721
Blog Entries: 1

Rep: Reputation: Disabled
Perhaps you should consider running iPython. From one side it is Python interpreter - from other works like system command shell.
 
Old 01-05-2021, 09:12 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,415

Rep: Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644
Quote:
Originally Posted by rincon View Post
hello guys good day
first of all: Happy New Year to everybody! I hope this year will not be a Covid year.

i want to change foo.txt to bar.txt in other words renaming a file using Python want to change
Code:
foo.txt to bar.txt
generally spoken - we could make use of this little one-liner:
Code:
import os
os.rename(r'file path\the old [former] name of the file name.file type',r'file path\the new file name.file type')
that means:
Code:
import os
os.rename(r'C:\Users\butch_miller\the_butch_desktop\Test\foo.txt',r'C:\Users\butch_miller\the_butch_desktop\Test\bar.txt')
by the way:
on a sidenote i have another question: Is this always necessary to use the character "r" before the so called path declaration in this python one-liner? Well, I was wondering why would I need to make use of r before the path in our little python one-liner? Guess that this is becaue of the'\' in the path?

Is there any other way to give path instead of using r'' Well in other words: do i need the syntax like this in python code:
Code:
  import os
    os.chdir(r'C:\Users\butch_miller\the_butch_desktop')
some musings have led to the following thoughts:

isnt it like so:
- Windows-os allways does understand both of the above mentioned syntaxes of our one-liner: this expression "" and that expression "/"
- that said it would be possible to run the line whilst using "/" : would this be possible?!
i guess that i now would be able to avoid 'r' letter before the rest of the path string.

hmmm - is there any shorter way to do the job of renaming a file using Python with a little one-liner? _I guess that the various versions of Python give us many many options so that we probably could write a shorter code. And probably a renaming of a bunch of files would not be too complicated too?
You have been working with Python for SEVEN YEARS now:
https://www.linuxquestions.org/quest...-;-4175467064/
https://www.linuxquestions.org/quest...db-4175509913/
https://www.linuxquestions.org/quest...ne-4175516522/

....so a trivial file rename shouldn't be complicated for you, sayhello_to_the_world. If you want to post your 'musings', post them on a blog. Again, after seven years of working with Python, you should have at least READ SOME DOCUMENTATION, which tells you that the 'r' is needed for unicode handling.

::EDIT:: Odd that you looked up/knew how to do this, but couldn't do a basic bit of research on things...where you would see the *EXACT COMMAND* you used, along with an explanation:
https://datatofish.com/rename-file-python/
Quote:
i will digg deeper and come back if i have found out and tested more options.
I doubt that, since you've said many, MANY times over the years you will 'come back and post all of your findings' (see this thread):
https://www.linuxquestions.org/quest...2/#post6147263

...where you have posted none of your 'findings'. Any reason you created a second user ID, and why do you think no one has noticed??

Last edited by TB0ne; 01-05-2021 at 09:24 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Looking for a sed one-liner to do one of two things oldpink Linux - Newbie 15 10-11-2019 10:46 PM
[SOLVED] Need a one-liner to change one digit in a variable brh1 Linux - General 3 02-15-2016 11:10 AM
perl one liner to delete line in csv file if one columns is toobig casperdaghost Linux - Newbie 2 01-15-2015 08:38 AM
Perl One Liner : Capturing more than one matched regex per line and printing uncle-c Programming 6 01-22-2012 05:16 AM
[SOLVED] Tar using include-file and exclude file one liner? metallica1973 Linux - Server 5 10-20-2011 03:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 04:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration