PYTHON - How to change one character in many file names within a directory

J Clifford

I have a large directory containing image files and need to change one character within the name of each file name. The character is in the same place in each file name (17). I think I need to use the 'replace' string function but as I am very new to Python I am not sure how to write this in a script (I work in GIS and have just started learning Python). Any help would be greatly appreciated. The character I need to change is the '1' after Nepal_Landscape_S'in the file name Nepal_Landscape_S1_LayerStacked_IM9_T44RQR_stack4.tif I simply need to change this to 2, like: Nepal_Landscape_S2_LayerStacked_IM9_T44RQR_stack4.tif

Micah Smith

You can use the string replace method as you suspect along with os.rename.

import os
files = os.listdir("path/to/files")
for src in files:
    dst = src.replace('S1', 'S2')
    os.rename(src, dst)

If you are able to use your shell for this type of task, there may be simpler solutions, like the rename bash command: rename S1 S2 *S1*

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python - How to read file one character at a time?

From Dev

Function that search for one character, then count how many times it appears Python

From Dev

Python - Read file names in directory, write twice to text file (one without file extensions), and separate with pipe

From Dev

Python: how to search for specific "string" in directory name (not individual file names)

From Dev

change file names in a directory with a certain pattern at the beginning

From Dev

How to check if a file is within a directory

From Dev

How to use paste command for many files whose names are not numbers? (paste columns from each file to one file)

From Dev

How to change one character into another character?

From Dev

Python- How to change the file name of a variable within a function

From Dev

remove date from file names for many files in a directory

From Dev

How to read in one character at a time from a file in python?

From Dev

How to copy and add prefix to file names in one step from another directory?

From Dev

How to add space in many file names

From Dev

How to change names of files in a directory to random generated names?

From Dev

How to build a list of file names in a local directory?

From Dev

How to zip directory with encryption for file names?

From Dev

How to determine how many files are within a directory without counting?

From Dev

C++ How to put the names of files within a directory into a vector?

From Dev

How to change multiple column names at one time?

From Dev

How to copy one directory to many directory using Linux command?

From Dev

Windows 10 Batch file to change directory names and move directories

From Dev

How to change one character to string in string variable

From Dev

How to change one character of an std::string?

From Dev

python change file names to unicode chars Hindi

From Dev

How many files in a directory before the size of the directory file increase

From Dev

Python: Print file names and their directory based on their file size

From Dev

Python: Print file names and their directory based on their file size

From Dev

How to change python packages directory

From Dev

How to change python packages directory

Related Related

  1. 1

    Python - How to read file one character at a time?

  2. 2

    Function that search for one character, then count how many times it appears Python

  3. 3

    Python - Read file names in directory, write twice to text file (one without file extensions), and separate with pipe

  4. 4

    Python: how to search for specific "string" in directory name (not individual file names)

  5. 5

    change file names in a directory with a certain pattern at the beginning

  6. 6

    How to check if a file is within a directory

  7. 7

    How to use paste command for many files whose names are not numbers? (paste columns from each file to one file)

  8. 8

    How to change one character into another character?

  9. 9

    Python- How to change the file name of a variable within a function

  10. 10

    remove date from file names for many files in a directory

  11. 11

    How to read in one character at a time from a file in python?

  12. 12

    How to copy and add prefix to file names in one step from another directory?

  13. 13

    How to add space in many file names

  14. 14

    How to change names of files in a directory to random generated names?

  15. 15

    How to build a list of file names in a local directory?

  16. 16

    How to zip directory with encryption for file names?

  17. 17

    How to determine how many files are within a directory without counting?

  18. 18

    C++ How to put the names of files within a directory into a vector?

  19. 19

    How to change multiple column names at one time?

  20. 20

    How to copy one directory to many directory using Linux command?

  21. 21

    Windows 10 Batch file to change directory names and move directories

  22. 22

    How to change one character to string in string variable

  23. 23

    How to change one character of an std::string?

  24. 24

    python change file names to unicode chars Hindi

  25. 25

    How many files in a directory before the size of the directory file increase

  26. 26

    Python: Print file names and their directory based on their file size

  27. 27

    Python: Print file names and their directory based on their file size

  28. 28

    How to change python packages directory

  29. 29

    How to change python packages directory

HotTag

Archive