How to Set the Current Working Directory

How to set the current working directory?

Try os.chdir

os.chdir(path)

        Change the current working directory to path. Availability: Unix, Windows.

How to change working directory in Jupyter Notebook?

Running os.chdir(NEW_PATH) will change the working directory.

import os
os.getcwd()
Out[2]:
'/tmp'
In [3]:

os.chdir('/')
In [4]:

os.getcwd()
Out[4]:
'/'
In [ ]:

How to change the current working directory?

copies it to the working directory of Qt

Not sure what exactly you mean by "Qt" in this context. If it is where the library is installed, you should associate that path with the file name then to be processed rather than setting the current working directory to be fair.

But why do you want to change the working directory at all? While you may want to solve one problem with it, you might instantly introduce a whole set of others. It feels like the XY problem. I think you will need a different solution in practice, like for instance the aforementioned.

If you still insist on changing the current working directory or whatever reason, you can use this static method:

bool QDir::​setCurrent(const QString & path)

Sets the application's current working directory to path. Returns true if the directory was successfully changed; otherwise returns false.

Therefore, you would be issuing something like this:

main.cpp

#include <QDir>
#include <QDebug>

int main()
{
qDebug() << QDir::currentPath();
if (!QDir::setCurrent(QStringLiteral("/usr/lib")))
qDebug() << "Could not change the current working directory";
qDebug() << QDir::currentPath();
return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

"/tmp/stackoverflow/change-cwd"
"/usr/lib"

change current working directory in python

I think a few things may be helpful.

It looks like you're on a windows system, so you should use double back slashes '\\' to separate the folders.

Second, if you're trying to change to a folder within the current folder, you should use a single dot, and not two, e.g. os.chdir('.\\folder')

Finally, if the folder you are trying to access is not a direct subfolder of the current working directory (or otherwise in your path), you need to include the full path to access it. Since you said it's on your desktop, you'd probably want something that looks like this:

import os
os.chdir('C:\\Users\\username\\Desktop\\headfirstpython') ## Where username is replaced with your actual username

From here, you could also change directories to the chapter3 subdirectory with the following

os.chdir('chapter3') 

Which is equivalent in this case with

os.chdir('.\\chapter3')

or, if you want to be wordy:

os.chdir('C:\\Users\\username\\Desktop\\headfirstpython\\chapter3')

Hopefully that helps?

How do I permanently set the current directory to the Desktop in Python?

You can add the line to your PYTHONSTARTUP file. So when you start an interpreter os.chdir("C:/Users/Name/Desktop") will be run.

I have a startup.py in my home directory file with the following content:

print("(.startup.py)")
import datetime, os, pprint, re, sys, time
print("(imported datetime, os, pprint, re, sys, time)")

So every time I start ipython or a python shell those lines are executed.

Not 100 percent but I imagine setting the PYTHONSTARTUP="path_to_script" in your environment variables on windows should do the trick with the two lines in your question in the startup file.

So for your situation you can create a file lets call it startup.py and inside that file you put:

import os
os.chdir("C:/Users/Name/Desktop")

Then the steps to add environment variable PYTHONSTARTUP:

For windows 8:

From the Desktop, right-click the very bottom left corner of the screen to get the Power User Task Menu.

From the Power User Task Menu, click System.
Click the Advanced System Settings link in the left column.

Under System variables, click New.

Add PYTHONSTARTUP to Variable name.

Add the path of the Python file to Variable value and click OK. # <-path_to_startup.py

Click OK.

How can I set the current working directory to the directory of the script in Bash?

#!/bin/bash
cd "$(dirname "$0")"

How to set current working directory in python in a automatic way

The right solution is not to change the current working directory, but to get the full path to the directory containing your script or module then use os.path.join to build your files path:

import os
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))

# then:
myfile_path = os.path.join(ROOT_PATH, "myfile.txt")

This is safer than messing with current working directory (hint : what would happen if another module changes the current working directory after you did but before you access your files ?)

Change current working directory in Azure Pipelines

It took many hours to find the solution, but, apparently, there is a way to specify working directory for specific script -
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/command-line?view=vsts&tabs=yaml :

- script: # script path or inline
workingDirectory: #
displayName: #
failOnStderr: #
env: # mapping of environment variables to add


Related Topics



Leave a reply



Submit