0

My script is under the directory:

'C:\\Users\\rikesh.kayastha\\project1\\daas\\src'

My script code is :

file_name_csv = "sample.csv"

os.chdir('C:\\Users\\rikesh.kayastha\\project1\\data')
df.to_csv(file_name_csv,index=False,encoding="utf-8")

This code saves the csv file in my desired directory. But this is just for local machine. How to adjust this without mentioning the local path. The base path is just project1 I want to remove the C:\\Users\\rikesh.kayastha\\ part so that this code will work on every machine.

2
  • " want to remove the C:\\Users\\rikesh.kayastha\\ part" Okay, so, just... do exactly that? It's a literal string in your code, you can just edit it so that there is only the remaining part, i.e. project1\\data. What actually is the difficulty? Do you understand what absolute and relative paths are? Do you understand what a current working directory is? The only real question I can see here is about how the computer works (specifically, how file paths work), not about how to write Python code. Commented May 19, 2022 at 6:41
  • "so that this code will work on every machine." What should happen when you run the code on another machine? Where should the file go? What is the rule that tells you where to put the file? Commented May 19, 2022 at 6:44

5 Answers 5

2
import os 
cudir = os.getcwd()
additional_dir = "\\project1\\data"
newdir = os.path.join(cudir, additional_dir)
print(newdir)
Sign up to request clarification or add additional context in comments.

1 Comment

os.getcwd() brings the user working directory, which might be different from the script directory
2

You can use sys.argv[0] to get the location of the script on the local machine, which should allow you to locate the data directory aswell

Comments

1

You can get the user's home directory using pathlib with Path.home().

from pathlib import Path

file_name_csv = "sample.csv"

user_home_directory = Path.home()
project_directory = user_home_directory / "project1"

output_filepath = project_directory / file_name_csv
df.to_csv(output_filepath, index=False, encoding="utf-8")

Comments

0

From which directory are you opening your file?

If it is

C:\roboczy\PythonScripts\test\project1\daas\src>python main.py

Then you can use:

filename = "sample.csv"
df.to_csv(f'../../data/{filename}',index=False,encoding="utf-8")

If you are ruinning scripts from project1 folder:

C:\roboczy\PythonScripts\test\project1>python daas\src\myscript.py

Then you can simply use:

filename = "sample.csv"
df.to_csv(f'data/{filename}',index=False,encoding="utf-8")

Comments

-1

Use pathlib library.

from pathlib import Path 
file_name_csv = "sample.csv"
current_dir = Path(__file__).parent   # returns the folder where your python file is
df.to_csv(current_dir.joinpath(file_name_csv), index=False,encoding="utf-8")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.