Removing a period from a several file names using Python - python

I've found several related posts to this but when I try to use the code suggested I keep getting "The system cannot find the file specified". I imagine it's some kind of path problem. There are several folders within the "Cust" folder and each of those folders have several files and some have "." in the file name I need to remove. Any idea what I have wrong here?
customer_folders_path = r"C:\Users\All\Documents\Cust"
for directname, directnames, files in os.walk(customer_folders_path):
for file in files:
filename_split = os.path.splitext(file)
filename_zero = filename_split[0]
if "." in filename_zero:
os.rename(filename_zero, filename_zero.replace(".", ""))

When you use os.walk and then iterate through the files, remember that you are only iterating through file names - not the full path (which is what is needed by os.rename in order to function properly). You can adjust by adding the full path to the file itself, which in your case would be represented by joining directname and filename_zero together using os.path.join:
os.rename(os.path.join(directname, filename_zero),
os.path.join(directname, filename_zero.replace(".", "")))
Also, not sure if you use it elsewhere, but you could remove your filename_split variable and define filename_zero as filename_zero = os.path.splitext(file)[0], which will do the same thing. You may also want to change customer_folders_path = r"C:\Users\All\Documents\Cust" to customer_folders_path = "C:/Users/All/Documents/Cust", as the directory will be properly interpreted by Python.
EDIT: As intelligently pointed out by #bozdoz, when you split off the suffix, you lose the 'original' file and therefore it can't be found. Here is an example that should work in your situation:
import os
customer_folders_path = "C:/Users/All/Documents/Cust"
for directname, directnames, files in os.walk(customer_folders_path):
for f in files:
# Split the file into the filename and the extension, saving
# as separate variables
filename, ext = os.path.splitext(f)
if "." in filename:
# If a '.' is in the name, rename, appending the suffix
# to the new file
new_name = filename.replace(".", "")
os.rename(
os.path.join(directname, f),
os.path.join(directname, new_name + ext))

You need to use the original filename as the first parameter to os.rename and handle the case where the filename didn't have a period in the first place. How about:
customer_folders_path = r"C:\Users\All\Documents\Cust"
for directname, directnames, files in os.walk(customer_folders_path):
for fn in files:
if '.' in fn:
fullname = os.path.join(directname, fn)
os.rename(fullname, os.path.splitext(fullname)[0])

Related

Rename the filename using python

I have folder where I have multiple files. Out of this files I want to rename some of them. For example: PB report December21 North.xlsb, PB report November21 North.xslb and so on. They all have a same start - PB report. I would like to change their name and leave only PB report and Month. For example PB report December.
I have tried this code:
import os
path = r'C://Users//greencolor//Desktop//Autoreport//Load_attachments//'
for filename in os.listdir(path):
if filename.startswith("PB report"):
os.rename(filename, filename[:-8])
-8 indicates that I want to split the name from the end on the 8th character
I get this error:
FileNotFoundError: [WinError 2] The system cannot find the file specified
Any suggestion?
You need the path when renaming file with os.rename:
Replace:
os.rename(filename, filename[:-8])
With:
filename_part, extension = os.path.splitext(filename)
os.rename(path+filename, path+filename_part[:-8]+extension)
The problem is likely that it cannot find the file because the directory is not specified. You need to add the path to the file name:
import os
path = r'C://Users//greencolor//Desktop//Autoreport//Load_attachments//'
for filename in os.listdir(path):
if filename.startswith("PB report"):
os.rename(os.path.join(path, filename), os.path.join(path, filename[:-8]))
This is a classic example of how working with os/os.path to manipulate paths is just not convenient. This is why pathlib exists. By treating paths as objects, rather than strings everything becomes more sensible. By using a combination of path.iterdir() and path.rename() you can achieve what you want like:
from pathlib import Path
path = Path(r'your path')
for file in path.iterdir():
if file.name.startswith("PB report"):
file.rename(file.with_stem(file.stem[:-8]))
Note that stem means the filename without the extension and that with_stem was added in Python 3.9. So for older versions you can still use with_name:
file.rename(file.with_name(file.stem[:-8] + file.suffix))
Where suffix is the extension of the file.

os.path.splitext() not working efficiently causing incorrect extension output

I am trying to split file name and file extension at specific path I am facing a problem the code is working fine in a certain way but at some point os.path.splitext() is not efficient at this point. the method is not splitting extension correctly, it's splitting the text and this cause an incorrect list for me.
This is the output when I run the code:
['', '.pdf', '.txt', '.docx', '.jpg', '.4000 + KeyGen', '.lnk', '.3', '.9', '.ini', '.png', '.url', '.exe', '.JPG', '.PNG', '.38)', '.Trainer', '.1 + Crack', '.ME]', '.pptx']
Notice there are some empty strings and some start with .ME although I don't have any extension at the path ends .ME or .9 etc.
What I can do is ether clean up my list with more code or adjust something in for loop to suit my needs.
Note that I am new to programming I am trying to learn as much as I can I am doing this for one week or so thanks for helping in advance.
import os, shutil
path = "C:\\Users\\VERX\\Desktop"
directories = []
file_names = []
# Split file name from file extension and append each one of them to its list.
for entry in os.listdir(path):
file_name, file_type = os.path.splitext(entry)
if file_type not in directories:
directories.append(file_type)
file_names.append(entry)
print(directories)

Programmaticallly moving files in python

I'm trying to simply move files from folder path1 to folder path.
import os
import shutil
path1 = '/home/user/Downloads'
file_dir = os.listdir(path1)
fpath = '/home/user/music'
for file in file_dir:
if file.endswith('.mp3'):
shutil.move(os.path.join(file_dir,file), os.path.join(fpath, file))
... but I get this error
TypeError: expected str, bytes or os.PathLike object, not list
First of all, you shouldn't use file as a variable name, it's a builtin in python, consider using f instead.
Also notice that in the shutil.move line, I've changed your (os.path.join(file_dir,f) to (os.path.join(path1,f). file_dir is a list, not the name of the directory that you're looking for, that value is stored in your path1 variable.
Altogether, it looks like this:
import os
import shutil
path1 = '/home/user/Downloads'
file_dir = os.listdir(path1)
fpath = '/home/user/music'
for f in file_dir:
if f.endswith('.mp3'):
shutil.move(os.path.join(path1,f), os.path.join(fpath, f))
You have confused your variable purposes from one line to the next. You've also over-built your file path construction.
You set up file_dir as a list of all the files in path1. That works fine through your for command, where you iterate through that list. The move method requires two file names, simple strings. Look at how you construct your file name:
os.path.join(file_dir,file)
Remember, file_dir is a list of files in path1. file is one of the files in that list. What are you trying to do here? Do you perhaps mean to concatenate path1 with file?
NOTE: Using pre-defined names as variables is really bad practice. file is a pre-defined type. Instead, use f or local_file, perhaps.
Read carefully the error message. file_dir is list. You can not join it with os.path.join. You probably want to write:
shutil.move(os.path.join(path1, f), os.path.join(fpath, f))
I suggest to name variables with meaningful names like:
file_list = os.listdir(path1)
This way you will not join a file list with a path :)

Change suffix of multiple files

I have multiple text files with names containing 6 groups of period-separated digits matching the pattern year.month.day.hour.minute.second.
I want to add a .txt suffix to these files to make them easier to open as text files.
I tried the following code and I I tried with os.rename without success:
Question
How can I add .txt to the end of these file names?
path = os.chdir('realpath')
for f in os.listdir():
file_name = os.path.splitext(f)
name = file_name +tuple(['.txt'])
print(name)
You have many problems in your script. You should read each method's documentation before using it. Here are some of your mistakes:
os.chdir('realpath') - Do you really want to go to the reapath directory?
os.listdir(): − Missing argument, you need to feed a path to listdir.
print(name) - This will print the new filename, not actually rename the file.
Here is a script that uses a regex to find files whose names are made of 6 groups of digits (corresponding to your pattern year.month.day.hour.minute.second) in the current directory, then adds the .txt suffix to those files with os.rename:
import os
import re
regex = re.compile("[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+[.][0-9]+[.][0-9]+")
for filename in os.listdir("."):
if regex.match(filename):
os.rename(filename, filename + ".txt")

Need to write multiple csv files to new folder

I haven't been able to find any information on this topic here, and would really appreciate your help! I'm pretty new to python, but here's what I have.
I have multiple file in a folder, and want to read them, transpose them, and then rewrite them into a new folder. I think I have everything going, but can't figure out how to rewrite everything.
here is my code:
path = 'C:\Users\Christopher\Documents\Clemson\Pleurodires\stability data\Es03\fixed\processed'
filenames = glob.glob(path + "/*.csv")
for filename in filenames:
dfs = (pd.read_csv(filename))
df = dfs.transpose()
df.to_csv('transposed\' + 'Tr_' + filename)
the last line (i hope) should put all the new files in the folder called 'transposed', adding a Tr_ in front of the name which was loaded initially (ie if the file name was 'hello' it would now be 'Tr_hello' inside of the folder transposed).
What is happening when I run the code above, is that it says it works, but then the files don't exist anywhere in my computer. I've tried playing around with a variety of different ways to get the df.to_csv to work and this is the closest I've gotten
Edit
Thanks for everyone's help, I ended up combining a mix of Nanashi's and EdChun's code to get this, which works: (the final files are in the correct folder, and are called Tr_filename)
path = r'C:\Users\Christopher\Documents\Clemson\Pleurodires\stability data\Es03\fixed\processed'
filenames = glob.glob(path + "/*.csv")
for filename in filenames:
short = os.path.split(filename)
newfilename = 'Tr_%s' % short[-1]
#print newfilename
dfs = (pd.read_csv(filename))
df = dfs.transpose()
df.to_csv(os.path.join('transposed', newfilename))
A few things:
filenames = glob.glob(path + "/*.csv") -- unless I'm wrong, that should be a backslash, not a forward-slash. Forward slashes are primarily used in Unix systems, etc. but definitely not in Windows where path names are concerned.
Try printing out filename. It will give you the whole path as well. At the df.to_csv line, you're actually writing to path + filename + transposed + Tr + filename. You have to isolate the specific filename (using split or the os module may work).
I'm using Ubuntu, so this might not apply that accurately, but here's how I'll do it.
import pandas as pd
from glob import glob
path = "/home/nanashi/Documents/Python 2.7/Scrapers/Scrapy/itbooks"
filenames = glob(path + "/*.csv")
for filename in filenames:
specname = filename.split("/")[-1]
print filename
print specname
dfs = pd.read_csv(filename)
df = dfs.transpose()
df.to_csv("transposed/%s" % specname)
Result:
/home/nanashi/Documents/Python 2.7/Scrapers/Scrapy/itbooks/realestateau.csv
realestateau.csv
/home/nanashi/Documents/Python 2.7/Scrapers/Scrapy/itbooks/itbooks.csv
itbooks.csv
[Finished in 0.6s]
Screenshot of transposed file:
Let us know if this helps.
Your code seems to have multiple errors try the following:
import os
path = r'C:\Users\Christopher\Documents\Clemson\Pleurodires\stability data\Es03\fixed\processed'
filenames = glob.glob(path + "/*.csv")
for filename in filenames:
dfs = (pd.read_csv(filename))
df = dfs.transpose()
df.to_csv(os.path.join(r'transposed\Tr_', filename))

Categories

Resources