Remove a List of Words from Filename

Remove a list of words from filename

You can access a file's name as a variable using the 'echo' command. Once that is done, the most powerful way to make your desired changes is to use 'sed'. You can string together sed commands with the '-e' flag. As part of a for loop in bash, this line gives you a start. You can also use a line like this as part of your 'find' statement.

echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI'

Once you have your desired file names, you can then move them back to the original name. Let me know if you need more specific instructions.

UPDATE: Here is a more complete solution. You'll have to tune the script to your own file names, etc.

for fyle in $(find . -name "*.*")
do
mv -i $fyle `echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI' `
done

Finally, to replace more than one whitespace character with one whitespace character, you can add another sed command. Here is a working command:

echo "file    input.txt" | sed 's/  */ /g'

File Handling in C - Removing specific words from a list in text file

  • You open two files: the one you've got (for reading) and a new one (for
    writing).
  • You loop through the first file reading each line in turn.
  • You compare the contents of each line with the words you need to
    delete.
  • If the line does not match any of the deletion words, then
    you write it to the new file.

If the manipulation that you need to do is much more complex then you can literally "read it into memory" using mmap(), but that is a more advanced technique; you need to treat the file as a byte array with no zero terminator and there are lots of ways to mess that up.

Remove certain strings from filenames inside a folder

I think I know what's wrong. filename will contain just the file's name, not a complete path. Try this:

dir = '/Users/CodingStark/folder/'
for filename in os.listdir(dir):
os.rename(dir + filename, dir + filename.replace('.pdf', ''))

Delete keywords from filenames in python

To replace a specific keyword, you can just use the string replace function:

import os
rootdir = r'C:\Users\Hemant\Desktop\testfiles'
str = "example.com"
for filename in os.listdir(rootdir):
if str in filename:
filepath = os.path.join(rootdir, filename)
newfilepath = os.path.join(rootdir, filename.replace(str, ""))
os.rename(filepath, newfilepath)

remove characters from every file name in directory with python

EDIT: To make code Generic and rename file names from one directory to any other directory/folder try following. I have kept this program inside /tmp and renamed the files inside /tmp/test and it worked fine(in a Linux system).

#!/usr/bin/python3
import os
DIRNAME="/tmp/test"
files = os.listdir(DIRNAME)
for f in files:
if '.vcf' in f:
newname = f.split('_')[0]
newname = newname + '.vcf'
os.rename(os.path.join(DIRNAME,f), os.path.join(DIRNAME,newname))



Since you want to rename the files, so we could use os here. Written and tested with shown samples in python3, I have given DIRNAME as /tmp you could give your directory where you want to look for files.

#!/usr/bin/python3
import os

DIRNAME="/tmp"
files = os.listdir(DIRNAME)
for f in files:
if '.vcf' in f:
newname = f.split('_')[0]
newname = newname + '.vcf'
os.rename(f, newname)

Removing Custom-Defined Words from List - Python

You need to use

df['filtered'] = df['tags'].apply(lambda x: [t for t in nltk.word_tokenize(x) if t not in filter_words])

Note that nltk.word_tokenize(x) outputs a list of strings so you can apply a regulat list comprehension to it.



Related Topics



Leave a reply



Submit