Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Does python have good text editing capabilities?

null_reflections

Legendary Coder
I'm curious since python has such broadband capabilities, I was pretty pleased with sed (a bash program that allows fairly easy text manipulation), yet I've been assured that perl and awk are better than sed, but neither of those languages seem terribly appealing.
 
i am not 100% sure what you are after BUT you could do something like this, This would print 1-400 with b at the end

Code:
for i in range(401):
    if i != 0:
       print(str(i)+"b")

then you could use something like this to show all of the files in your current folder:-
Code:
import os

arr = os.listdir()
for file in arr:
    print(file)

but if you wanted to take all the files in a folder and search for say 1b.txt ,2b.txt and change them to 1like.txt ,2like.txt you could do something like this :-
Code:
import os

arr = os.listdir()

#Here we loop
for i in range(401):
    #if i is not 0 carry on
    if i != 0:
        #load all files
        for file in arr:
            filename = str(i) + "like.txt"
            print(filename)
            #if the filename contains the number change and break
            if filename in file:
                os.rename(file, filename)
                break

python is amazing :D
 
To add to simong1993 last example you could also use string.replace.

Python:
import os

# List directory
files = os.listdir()

# Loop through the files
for file in files:

    # Check the file ext. for all text files
    if file[-3:] == 'txt':

        # Rename file using string replace
        renamed_file = file.replace('b', 'a')
        os.rename(file, renamed_file)
 
Last edited:
there is one major issue with using .replace
Code:
renamed_file = file.replace('b', 'a')

lets say i have:-
100b.txt that will become 100a.txt
but now lets say i have:-
thebeeinthebee.txt this will now become theaeeintheaee.txt

.replace will replace all of the letters b with a's :S
 
there is one major issue with using .replace
Code:
renamed_file = file.replace('b', 'a')

lets say i have:-
100b.txt that will become 100a.txt
but now lets say i have:-
thebeeinthebee.txt this will now become theaeeintheaee.txt

.replace will replace all of the letters b with a's :S
But surely this can be fixed by using a regular expression ?
 
Agreed but was just going by op's example of changing one character in a numberletter filename. Could use an input and rename the whole filename.
Could even implement sed in the program to do the changes.

Another example from the above. This uses a list of file extensions not just txt

Python:
import os

# List directory
files = os.listdir()

# Loop through the files
for file in files:

    # Check the file ext.
    ext = ['txt', 'png', 'gif', 'jpeg', 'xls']
    if file[-3:] in ext:

        # Rename file using string replace
        renamed_file = file.replace('b', 'a')
        os.rename(file, renamed_file)
 
Slight problem with your code, `listdir` lists folders as well as files so you need to check if each object is a file. Also, `file[-3:]` never would equal `jpeg` since jpeg is four characters long. I would fix that by using `.endswith`. However, l used `splittext` to get the file extensions since it accounts for multiple possible extensions in the file name.

Python:
from os import listdir
from os.path import isfile, splittext

# List directory
files = [isfile(file_path) for file_path in os.listdir()]

exts = ['.txt', '.png', '.gif', '.jpeg', '.xls']

# Loop through the files
for file_path in files:
    file_ext = splittext(file_path)[-1]
 
    # Check the file ext.
    if any([file_ext == ext for ext in exts]):
        # Rename file using string replace
        renamed_file = file_path.replace('b', 'a')
        os.rename(file_path, renamed_file)
 
Last edited:

New Threads

Buy us a coffee!

Back
Top Bottom