In a terminal I can move around simply using:
cd [file path]
However I need a script that does this automatically as I need to go into a a bunch of folders (around 200) and run a programme in each one.
So far I've tried things like shutil.move() which does not do what I need as that move the entire folder my code is in, including my IDE which causes my programme to crash.
Is there any way to run a script in python that opens up a terminal shell, navigates to a folder via a file path and then executes a programme when in that folder?
So for this, you could use the os module. Then do this:
for dir in os.listdir():
if os.path.isdir(dir):
os.chdir(dir)
# Then do whatever program you wish to run
Related
For background, I am using the Atom editor with the script package installed.
Within Atom, there are two projects that I normally use.
D:/Data_files/Git/Projects
D:/Data_files/Python/Notes
When I normally open atom from the terminal, I have both projects open, and for whatever reason it has the default project or something set to the Git/Projects project
The Git/Projects directory is not important, but here is what the Python directory looks like.
D:/Data_files/Python/Notes/
__init__.py
files_and_exceptions.py
text.txt
Here's what text.txt contains
This is the text of text.txt
Here is what files_and_exceptions.py contains
import os
print(os.getcwd)
os.chdir("../../Python/Notes")
print(os.getcwd)
with open('text.txt') as hello:
contents = hello.read()
print(contents)
Which outputs when I run it using CTRL+SHIFT+B from the files_and_exceptions.py using script
D:/Data_files/Git/Projects
D:/Data_files/Python/Notes
This is the text of text.txt
So even though I'm running it from the Python/Notes directory, the current working directory is set to Git/Projects. I want files_and_exceptions.py to print text.txt without importing and changing directories when running from the file because that's how it would work when I run it normally using Python's Shell. I know I can set it by opening Atom from the directories in separate windows but that gets irritating when I'm working with multiple projects at once. Is there any way to easily change the current working directory of Atom when working between projects or modify the script package to change the current working directory to the directory of the open tab (ie: files_and_exceptions.py) when I run it with CTRL+SHIFT+B?
In the default settings, script uses the project directory of the first project in your Project tree. In the settings of script, you can change the that the project directory of your current script (or the directory your script resides in itself).
You go to settings (press Ctrl+,), then the Packages tab, you search for the script package and the first dropdown menu is that setting.
If you don't want to change this, you can also have script run with a specific profile.
In the command palette Ctrl+Shift+p, you search for script: Run options and you get a menu with some settings to start your script with. You can save those as profile and later use script: Run with Profile to reload.
I am trying to run a python script, .py in the windows command prompt.
I drag the script from my files into the command prompt window. I run it.
Then, the script presents a prompt for me to enter the file I would like to parse. I am supposed to enter a file like this: filename.txt
When I go to do this, I get the FileNotFoundError. They are both located in my downloads folder. Any ideas as to why? I've tried a couple of things but having no luck.
The script must be performed this way because this is a script many people will use and it is likely that they will receive it and download it and the file to parse to their computer so it'll be located in their downloads folder.
When I created the script I did it in Notepad++ and had a separate folder where I put the file and script and it worked fine this way. I am just ensuring it works from the perspective of someone who's downloading it off an email or website, etc.
Thanks !
Yeah that happened to me a lot.
To solve it just write the whole directory of the file.
If you are in windows
c:\user\username\Desktop\file.txt
If you are in linux or mac:
/home/username/Desktop/file.txt
I was writing a program that accesses a .txt file at the start of the program with the open() function. It ran without any errors on the IDE and I was also able to read the text file when running from the IDE without any issues. Although when I ran from the Python Launcher it threw a "FileNotFoundError"
Here's my code:
directions_object = open('warcards_directions.txt','r')
Further to Dan D's comment.. try putting this on the line in front of your open() call:
from os.path import abspath
print(abspath('warcards_directions.txt'))
You'll see that python looks in different places depending on where you run it from .. because it looks for files relative to the current working directory, which changes depending on how you run python.
This is a common problem for new comers. See here How to import files in python using sys.path.append? for some solutions (note the underlying problem in that post is the same as this one.. the fact that they're trying to import a file, and here we're trying to open one is not too important).
Also I'll add that I often reference things relative to the script itself... like this:
from os.path import abspath, join, dirname
script_dir = dirname(__file__)
txt_path = abspath(join(script_dir, "..", "path", "to", "warcards_directions.txt"))
This works if your txt file and your python script stay in the same place relative to each other (but might be installed in different places).
E.g. above assumes your script lives in C:\Foo\scripts\script.py and your text file lives in C:\Foo\path\to\warcards_directions.txt. The method above will work fine where ever you run the script from and it'll work if you move or rename the C:\Foo dir (e.g. to C:\Program Files\Bar). But it'll break if you decide to move scripts.py down a directory into C:\Foo (at which point you change the way txt_path is initialized to fix).
When you said "python launcher", do you mean the command line?
python myScript.py
If you, you will need to cd into the directory where the file is at before you can execute the script. Otherwise, provide the full path to the txt file in your script.
I made a simple python script which creates a text file. The contents of the script are
f = open("New", "w")
f.close
Now I moved the script to the desktop (I'm on a mac) and ran it, and nothing shows up. No file is created on the desktop that is visible for me.
I actually made this little script because one of my other scripts involves opening/creating a text file and reading from it. I entered the information wrong while in my program forever, and now the entire thing is broken and I cant fix it because I have no idea where it's created the darn text file. I was under the impression that just opening a file with giving it an absolute path would create it in the same directory as the script. I seem to have been mistaken.
I've been launching the script from the terminal with the command
python3 /Users/me/Desktop/script.py
Because of that, I feel like its creating the file somewhere in the python3 install location or within wherever the python3 unix exec is located. I think. Can't check.
Are any of you guys willing to help out?
-pipsqueaker117
EDIT: Here's a link to the big program which broke.
It'll be created in the current working directory, which is the directory from which you called the script.
Your file will be created in the current directory (most probably in /Users/me/ if you just opened the terminal)
Try:
cd /Users/me/Desktop/
python3 /Users/me/Desktop/script.py
You should modify your program to change the working directory (before you write the file) to the place where you want files to show up.
import os
os.chdir('/Users/me/Desktop') # or whatever
This should be a directory where you have permission to write files.
You can always ask:
import os
cwd=os.getcwd()
print(cwd)
That will print the directory the file (without a path) will be opened in.
You can change to a specific directory (in Python) this way:
import os
try:
os.chdir('/Users/me/Desktop')
except OSError as e:
print e
I'm trying to use a python script to read statistics off of a file: savegame.dat
http://pastebin.com/DmsHfPhU is the script
The script came with instructions: "Use: python %script% %savegame.dat%"
I have python, I don't know where to put the savegame.dat file.
For some reason python launcher isn't working so I have to do it manually through the Terminal using the "python" command. I tried putting it in the same folder and it didn't work. I'd like to know what I need to change in the raw paste so it recognizes the savegame.dat. I tried dragging the script into the Terminal.app after opening the python command. With the script saved in the same place as .dat file.
Put it in the same directory as you are running the script in.
Save the paste in the link to a file. Call it foo.py. Put foo.py and the .dat file in the same directory.
Open the terminal.app. cd to the directory that both files are in, then type python foo.py savegame.dat
It doesn't matter where you put the file so long as you specify the full path to it on the command line.