What is the meaning of the getcwd() method?

200 Views Asked by At
import os 
cwd = os.getcwd()
df.to_csv(cwd+ "/BA_reviews.csv")

I didn't understand this code properly. Can someone help?

3

There are 3 best solutions below

0
Daweo On

os.getcwd docs

Return a string representing the current working directory.

so

import os 
cwd = os.getcwd()
df.to_csv(cwd+ "/BA_reviews.csv")

means read BA_reviews.csv from current working directory.

0
Farman Ali Ujjan On
import os

# os.getcwd() is a method in os module which gives the current directory
cwd = os.getcwd

# df is dataframe and to_csv is saving the df data in csv file 
#here file name is BA_reviews.csv
# you are basically concaneting the path and the filename 

df.to_csv(cwd+"/BA_reviews.csv")
0
JonDel On

Tha's just getting the absolute path of the current folder in order to save, from the pandas dataframe, a cvs file into that folder. You can either do the same using the built-in library specific for path operations, pathlib, and f-strings, in a more pythonic way:

import pathlib

cwd = pathlib.Path().absolute()
df.to_csv(f"{cwd}/BA_reviews.csv")