Python change strings line by line in all files within a directory

John

So I have some files located in directory. Some of the files contain paths like this and some are empty: C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module.c

What would be the best way to cut it just by counting backslashes from the end: So in this case we need to cut everything what is after 4th backslash when counting backward:

Folder1\Folder2\Folder3\Module.c

I need some function that will go through all files and do this on each line of a file.

Current code which do not work for some reason is:

directory = os.listdir(//path_to_dir//)
for file in directory:
    with open (file) as f:
        for s in f:
            print('\\'.join(s.split('\\')[-4:])) 
Yuri Khristich

I would try something like this:

from pathlib import Path

def change(s):
    return '\\'.join(s.split('\\')[-4:])

folder = Path.cwd() / "folder" # here is your folder with files
files = folder.glob("*")

for f in files:
    with open(f, "r") as file:
        content = file.read()

    lines = content.split('\n')
    new_lines = []

    for line in lines:
        new_lines.append(change(line))

    with open(f, "w") as file:
        file.write("\n".join(new_lines))

It look for all files in the subfolder folder, does replacing on every line of every file and saves the files.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I `find` files matching pattern within a directory matching another pattern from the command line?

Copy all files within directory into new directory while renaming files with same name in Python

Read all .txt files in a folder line by line

How to find a line of code and delete that line from all files in a certain /directory only, using Linux CLI

Change line in regex python

How to comment out a perticular line in all files in directory and sub directory using #ifdef and #endif?

Append Line to All Files in a Directory Recursively in Python

Change directory in pipe line bitbucket

How can I read all files line by line from a directory by using Streams?

How to remove the last line of all files from a directory?

Linux command line. Move all files and directories in directory, except some files and directories

Change Multi line strings to Single line

Rename all files in directory to line present in each file using python

How to prepend a line to all files in a directory?

How To Get Total Line Count From All Text Files In A Directory

Replace first line in directory files

Command line tool to rewrite all files in directory tree

Replace string within multiple files using per line strings from one file

get minimum and maximum line count from files within a directory

How to get the line-count of all the files in a directory

Grepping all strings on the same line

Convert all text files in directory to strings (each txt file becomes 1-line)

Searching for files, within a working directory (and subdirectories in it) containing strings on the same line

Python append line of strings to other line of strings

SED - Change a strings in the line, only if that line start with

Delete last line from multiple CSV files within a directory

Rename all xml files within a given directory with Python

Change "-" to "C" in all files within directory

Python - read all files in a directory, and multiply the value in a specific line