open csv from different directory in python - python

Im trying to open a csv file as followed
My ipynb-file is in the following directory -> /data/filename.ipynb
where as my csv file is in the following directory -> /data/preprocessed/processed_data.csv
When i try to open the file with the following code:
df = pd.read_csv('/preprocessed/processed_data.csv')
I get an exception that the file doesn't exist. Somehow I think I didn't quite understand how the directories work. Please help, thanks.

try this-
df = pd.read_csv('preprocessed/processed_data.csv')

Try using the os module with a relative path:
import os
filename = os.path.join(os.path.dirname(__file__),'path/to/file/processed_data.csv')
df = pd.read_csv(filename)

Related

Renaming a file in pyton using a variable

hey all hope someone can help!
Im using the below to find the most recent file downloaded in the specified directory. I need to be able to rename the file from how it comes to a specic name - any help would be appreciated.
import glob
import os
download_path = f'C:\\Users\\{os.getlogin()}\\Downloads'
list_of_files = glob.glob(f"{download_path}\\*")
latest_file: str = max(list_of_files, key=os.path.getctime)
print(latest_file)
os.rename("latest_file", "??????????")
the file needs to be called sf-admin-term
Thanks!
ext = latest_file.split('.')[-1]
os.rename(latest_file, f"{download_path}/new_file.{ext}")

VS Code: Is there a way to read a csv file without needing specification of the full path?

I am trying to read data from a csv file (in the same folder as my main.py) but it seems that
Visual Studio Code doesn't understand the project folder or something of the sort
FileNotFoundError: [Errno 2] No such file or directory: 'ratings.csv'
Here is my code
import numpy as np
import pandas as pd
# read data with panda, only the columns that are needed
r_cols = ['user_id', 'movie_id', 'rating']
ratings = pd.read_csv('ratings.csv', sep=';', names=r_cols, usecols=[1, 2, 3], encoding="ISO-8859-1", low_memory=False, header=0)
Adding the full path of the file fixes the problem, and it also works if I add
import os with os.chdir in the beginning of the code.
But PyCharm doesn't need the above tweaks in order to run it. So my question remains, is there a VSCode setting that I am missing?
I got the same issue and I solved it by doing this:
import pandas as pd
df = pd.read_csv('Pandas/sample.csv')
print(df)
As people mentioned in the comments, we can set the debug path in VSCode, please add the following settings in "launch.json": (It will automatically go to the directory where the file is located before debugging the code)
"cwd": "${fileDirname}",
import os
def infolder_file( filename ):
afname = os.path.abspath(__file__)
current_folder = os.path.dirname(afname)
uf = os.path.join(current_folder, filename )
return uf
print( infolder_file( 'anyfilename.txt' ) )
You can define a constant for the directory at the top of your module that you then use with any files you need to access.
from pathlib import Path
DIRNAME = Path(__file__).parent
def func():
fn = DIRNAME / 'file.suffix'

FILE NOT FOUND error in Anaconda

This code gives me a file not found error and I don't know if it is the file location that is giving me troubles. I'm new to Python.
import pandas as pd
df= pd.read_csv('breast-cancer-wisconsin.data.txt')
#file location = 'C:\Users\qeee11\Downloads\breast-cancer-wisconsin.data.txt'
df
import pandas as pd
df= pd. read_csv('breast-cancer-wisconsin.data.txt')
# Give commands to extract the directory like getcwd()
#like , data_path = os.path.join(os.getcwd(), 'data')
file location = 'C:\Users\qeee11\Downloads\breast-cancer-wisconsin.data.txt'
getcwd() details
Another solution is to use ! shell commands , to change the directory,
you can write commands like !cd , and !ls to change the directory to the current file.

FileNotFoundError while importing a csv file using pandas in Jupyter notebook

import pandas as pd
df = pd.read_csv('/home/josepm/Documents/test_ver2.csv')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-3-5cd7fd573fb7> in <module>()
1 import pandas as pd
----> 2 df = pd.read_csv('/home/josepm/Documents/test_ver2.csv')
I try to import a CSV file using pandas and every time it says that it doesn't find the file. It's like Jupyter doesn't see it. I tried to do this:
import os
os.path.isfile('/home/josepm/Documents/test_ver2.csv')
and it doesn't see the file either.
Change
pd.read_csv('\Users\user\Desktop\Workbook1.csv')
to
pd.read_csv(r'C:\Users\user\Desktop\Workbook1.csv')
Please try the following code:
import os
path = os.path.abspath(r'file path')
f = open(path)
print(f)
The working directory is the point from where all the files are accessed in Jupyter Notebook.
Find the current working directory
import os
os.getcwd()
Example o/p : 'C:\Users\xyz'
Now place your CSV files in this path
List the contents of your directory to check if the CSV file is present
os.listdir('C:\Users\xyz')
Now try reading the CSV file
Copy the file directory, then paste it here:
pd.read_csv(r'(here)\(csv file name including .csv)').
for example:
pd.read_csv(r'C:\Users\DCL\ML and related\Bengaluru_House_Data.csv')
Hope this will work.

Editing file names and saving to new directory in python

I would like to edit the file name of several files in a list of folders and export the entire file to a new folder. While I was able to rename the file okay, the contents of the file didn't migrate over. I think I wrote my code to just create a new empty file rather than edit the old one and move it over to a new directory. I feel that the fix should be easy, and that I am missing a couple of important lines of code. Below is what I have so far:
import libraries
import os
import glob
import re
directory
directory = glob.glob('Z:/Stuff/J/extractions/test/*.fsa')
The two files in the directory look like this when printed out
Z:/Stuff/J/extractions/test\c2_D10.fsa
Z:/Stuff/J/extractions/test\c3_E10.fsa
for fn in directory:
print fn
this script was designed to manipulate the file name and export the manipulated file to a another folder
for fn in directory:
output_directory = 'Z:/Stuff/J/extractions/test2'
value = os.path.splitext(os.path.basename(fn))[0]
matchObj = re.match('(.*)_(.*)', value, re.M|re.I)
new_fn = fn.replace(str(matchObj.group(0)), str(matchObj.group(2)) + "_" + str(matchObj.group(1)))
base = os.path.basename(new_fn)
v = open(os.path.join(output_directory, base), 'wb')
v.close()
My end result is the following:
Z:/Stuff/J/extractions/test2\D10_c2.fsa
Z:/Stuff/J/extractions/test2\E10_c3.fsa
But like I said the files are empty (0 kb) in the output_directory
As Stefan mentioned:
import shutil
and replace:
v = open(os.path.join(output_directory, base), 'wb')
v.close()
with:
shutil.copyfile (fn, os.path.join(output_directory, base))
If I'am not wrong, you are only opening the file and then you are immediately closing it again?
With out any writing to the file it is surely empty.
Have a look here:
http://docs.python.org/2/library/shutil.html
shutil.copyfile(src, dst) ;)

Categories