Python Data frame to CSV - Converting to Windows Filepath - python

The following code works for me on the MAC OS but I'm having trouble converting it to a windows file path. If I just make the forward slashes into back slashes the file turns to color red as if it is a literal.
dict=pd.DataFrame(elements_count, index=[0,])
dict.to_csv(path+'/Word_freq/'file+'.csv')
Thank you in advance for any help.

You can use os.path.join
import os
fullPath = os.path.join(path,'Word_freq', file + '.csv')
In fact, I'd always prefer os.path.join over string concatenation, cause it will join the path based on the platform
But if you still want to use string concatenation, then you can join the path, directory, subdirectory, or file name using separting them by by os.sep
Like this:
fullPath = path + os.sep + 'Word_freq' + os.sep + file + '.csv'

Related

Remove special character from multiple file names in folder in python

I am trying to remove special character(-) from multiple files in folder.
Example:
Filenames :
-name1.xml
-name2.xml
-name3.xml
Rename To:
name1.xml
name2.xml
name3.xml
My Code :
import os
for filename in os.listdir(Folder):
os.rename(Folder+'/'+filename, Folder + '/' + Filename.replace("-","" )
but Unfortunately it appears to do nothing.
How do I do this properly?
In the code below, you can simply use the replace function to get your desired output. Let me know if this works and/or helps!
import os
# I named my folder containing .XML files "Test"
Folder = 'Test/'
for filename in os.listdir(Folder):
os.rename(Folder + filename, Folder + filename.replace('-',''))
Outputs:
name1.xml
name2.xml
name3.xml
I am a big fan of using Pathlib for problems like this.
Given (on Unix but should work on all OSs):
% ls -1
-name1.xml
-name2.xml
-name3.xml
name-4.xml
You can do:
from pathlib import Path
p=Path(Folder)
for fn in p.glob("-*.xml"):
fn.replace(fn.with_name(str(fn.name).replace('-','')))
Result:
% ls -1
name-4.xml
name1.xml
name2.xml
name3.xml
The glob -*.xml only will find files that start with - so the file name-4.xml is unchanged.
Pathlib (as written here) will also work the same regardless of the path separator on different OSs.

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.

Using variable and strings to create name with posixpath

I want to create a path with python pathlib using a variable.
This is of course incorrect due to mixing of string and posixpath:
from pathlib import Path
stringvariable='aname'
Path(Path.cwd() / 'firstpartofname_' +stringvariable+ '.csv')
I know I could do it with os, or in two lines like this:
filename='firstpartofname_' + stringvariable + '.csv'
Path(Path.cwd() / filename)
but I want to learn how to use it directly with Path.
Thanks
You just need to add parentheses to force the + to happen before the /.
new = Path.cwd() / ('firstpartofname_' + stringvariable + '.csv')

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))

Removing a period from a several file names using 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])

Categories

Resources