My coworker is having trouble with a Python install. When running the code below, from 'C:\my\folder\', 'C:\' is returned instead of the current working directory. When I or anyone else run the script on our systems, we get 'C:\my\folder\'.
We're assuming that some global setting must be causing the issue, so I've had the person uninstall Python, delete the local Python2.7 folder, clean the registry and reinstall the Python, but it's still not working.
NOTE: We have a large number of legacy scripts, so revising all of them to use subprocess is impractical. :(
Any ideas?
Environment: Windows XP, Python 2.7
import os
#
# This test script demonstrates issue on the users computer when python invokes
# a subshell via the standard os.system() call.
#
print "This is what python thinks the current working directory is..."
print os.getcwd()
print
print
print "but when i execute a command *from* python, this is what i get for the current working directory"
os.system('echo %cd%')
raw_input()
you could also try something like this
os.chdir("C:\\to\\my\\folder")
print os.system("echo %CD%")
raw_input()
also to get the current working directory i use a different approach
cur_dir = os.path.abspath(".")
os.getcwd() isn't guarenteed to get the location of your script when it is called. Your coworker might be calling the script a different way or his computer (for some reason) handles the current working directory differently.
To get the actual script location you should use the following:
import os
os.path.dirname(os.path.realpath(__file__))
As an example I wrote getcwd and the above line in the same script and ran it from C:\.
Results:
C:\>python C:\Users\pies\Desktop\test.py
C:\Users\pies\Desktop
C:\
It depends on what your real purpose for this script is, whether you actually need the current working directory, or just the current scripts directory. As a little caveat this call will return a different directory if you call a script from script which then uses this call.
os.system("cd dir;command params")
Related
why running
import subprocess
# I'm going to run another python script in anaconda script like this:
#subprocess.call("C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat && python C:\test.py")
# but for simplifying the sample for here I've deleted the second part
# (in the case you didn't install python in the default path, you can replace v_activate_address with the correct address on your computer to see the result)
v_activate_address = "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"
subprocess.call(v_activate_address)
gives. (it's not an error):
The system cannot find the path specified.
This code can run my test.py in anaconda environment truly but that line appears which is not desirable.
How should I get rid of that?
[the address is correct please don't say that]
answer to #Dunes comment:
I wrote a simple python3.7 code like this :
import os
if __name__ == "__main__":
c = os.getcwd()
print(c)
This code path is ~/PyStudy/OsTest/test.py
When I ran it in VS Code and Terminal, different results appeared.
in Terminal, it returned: ~/PyStudy/OsTest
in VS Code, it returned: ~/PyStudy without /OsTest.
I used Code Runner in VS Code to run python code.
I don't know why, please help me.
Because they are running in different directories.
Apparently you started VS Code in the parent directory, and it simply keeps on running there until you terminate it.
Also apparently, you ran the script from the terminal by first doing a cd into this directory. There's no need to do that, though. Try
( cd /; python3 ~/PyStudy/OsTest/test.py )
at the Terminal prompt.
Generally speaking, the current working directory of a process is a convenience mechanism. By using relative paths, you can write shorter file names; but in most situations, you can use an absolute file name instead, from a process running in any directory.
If it's returning different values, you can be sure it's returning the correct values. The difference must be in the way you're running the program. Vs code probably sets its working directory to something different than what you were doing when running from a terminal.
The entry points for execution are different for both. That is why it is happening.
I have a Python script that tags MP3's and renames them. It works when assigning sys args via script parameters in Pycharm.
However, when I call the Python file from a bat, parsing in the same two parameters, I get problems with the file rename.
When printing os.listdir debugging the script, I see a bunch of old test MP3's that are no longer in the dir.
I've checked I'm referencing the correct dir. But it seems like somehow the list of files is cached and being passed over (since I have a validation check - abstracted function in the second part of the if statement blow).
Could it be possible Windows is caching something, or perhaps the .pyc is?
Here is part of the Python in question...
os.chdir(directory)
for files in os.listdir("."):
if files.endswith(".mp3") and not the_file_name_is_valid(files):
log.info("Starting work on: " + files)...
When run from the below bat file, os.listdir(".") produces a list of files that are not current for the working directory. Yet when the script is run from Pycharm it works perfectly.
Here is the bat
echo off
set SET_PREP="set_prep.py"
set DIR=%cd%
set IMAGE="cover_artwork.jpg"
c:\python27\python.exe %SET_PREP% %DIR% %IMAGE%
Pause
Why does running my script with command parameters behave differently to using Script Parameters in Pycharm?
So, I am following a simple tutorial "Python Tutorial: Automate Parsing and Renaming of Multiple Files" and am already encountering a problem where os.chdir() is not working. I am on a Windows 10 system, running python 3.6, and I have tried using both my regular terminal (which has cygwin installed) and bash on ubuntu on Windows.
Here is the code:
import os
print(os.getcwd())
os.chdir('c:/Users/Michelle Kaiser/Desktop/Lab_Progs/PI3Kalpha')
print(os.getcwd())
Here is the reg terminal:
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>python rename.py
C:\Users\Michelle Kaiser\Desktop\Lab_Progs
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>`
The path that it is returning corresponds to the folder my program is located in. I have moved the program 3 times to verify this. Also, it's obviously returning a path only once, so it's probably not responding to the 2 print statements.
Here is the bash terminal:
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$ python rename.py
/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$
I also tried running the code with os.path.exists(), which did not change the output on either terminal. I have definitely double checked that I am saving my program file from one test to the next. Thanks.
I've been trying to change a file that has no whitespace.
It seems like this person has a similar problem:
Python reading whitespace-separated file lines as separate lines
I'm trying to change the terminal directory through a python script. I've seen this post and others like it so I know about os.chdir, but it's not working the way I'd like. os.chdir appears to change the directory, but only for the python script. For instance I have this code.
#! /usr/bin/env python
import os
os.chdir("/home/chekid/work2/")
print os.getcwd()
Unfortunately after running I'm still in the directory of the python script (e.g. /home/chekid) rather than the directory I want to be in. See below.
gandalf(pts/42):~> pwd
/home/chekid
gandalf(pts/42):~> ./changedirectory.py
/home/chekid/work2
gandalf(pts/42):~> pwd
/home/chekid
Any thoughts on what I should do?
Edit: Looks like what I'm trying to do doesn't exist in 'normal' python. I did find a work around, although it doesn't look so elegant to me.
cd `./changedirectory.py`
You can't. The shell's current directory belongs to the shell, not to you.
(OK, you could ptrace(2) the shell and make it call chdir(2), but that's probably not a great design, won't work on Windows, and I would not begin to know how to do it in pure Python except that you'd probably have to mess around with ctypes or something similar.)
You could launch a subshell with your current working directory. That might be close enough to what you need:
os.chdir('/path/to/somewhere')
shell = os.environ.get('SHELL', '/bin/sh')
os.execl(shell, shell)
# execl() does not return; it replaces the Python process with a new shell process
The original shell will still be there, so make sure you don't leave it hanging around. If you initially call Python with the exec builtin (e.g. exec python /path/to/script.py), then the original shell will be replaced with the Python process and you won't have to worry about this. But if Python exits without launching the shell, you'll be left with no shell open at all.
You can if you cheat: Make a bash script that calls your python script. The python script returns the path you want to change directory to. Then the bash script does the acctual chdir. Of course you would have to run the bash script in your bash shell using "source".
The current working directory is an attribute of a process. It cannot be changed by another program, such as changing the current working directory in your shell by running a separate Python program. This is why cd is always a shell built-in command.
You can make your python print the directory you want to move to, and then call your script with cd "$(./python-script.py)". In condition your script actually does not print anything else.