Call variables from .py script into Batch - python

I have a series of .py files that I am using a .bat for to automate the whole process. Within some of the .py files I have called variables from another .py file. I did this to allow myself to only change one .py when running new processes. I have the .bat set up to run normally with the .py files, but I do not understand how to incorporate the variables.py file.
Example:
Python1.py
import os
import somefolder.variables as variables
path = 'C:/pictures/'
file = variables.file
for img in os.listdir(path + variables.file):
print(img)
Variables.py
file = item1.img
Batch.bat
call "C:/Users/name/anaconda3/Scripts/activate.bat" "C:/Users/name/anaconda3/envs/environment"
call "C:/Users/name/anaconda3/envs/environment/python.exe" "D:/Scripts/python1.py"
...
After running something similar to this. I received the error: No module named 'somefolder'. I've read a few other posts about using the echo command or using set, but it seems as that is setting a variable within the batch and not calling it from another .py file. I am farily new to batch files, so any help would be appreciated.

As #Rashid 'Lee' Ibrahim mentioned in the comments above, it would be best to look into using __intit__.py. Though for the sake of getting the code to run, I set where the module/.py file was located as a system path.
Python1.py Original
import os
import somefolder.variables as variables
path = 'C:/pictures/'
file = variables.file
for img in os.listdir(path + variables.file):
print(img)
Python1.py Edited
import os
sys.path.append('D:/Scripts/')
import variables
path = 'C:/pictures/'
file = variables.file
for img in os.listdir(path + variables.file):
print(img)

Read file contents into a variable:
for /f "delims=" %%x in (version.txt) do set Build=%%x
or
set /p Build=<version.txt
Both will act the same with only a single line in the file, for more lines the for variant will put the last line into the variable, while set /p will use the first.
Using the variable – just like any other environment variable – it is one, after all:
%Build%
So to check for existence:
if exist \\fileserver\myapp\releasedocs\%Build%.doc ...
Although it may well be that no UNC paths are allowed there. Can't test this right now but keep this in mind.

Related

Python script runs correctly from pycharm but not from batch file

I have a python script that scans some csv files from a directory, gets the last line from each, and adds them all in a new csv file. When running the script inside pycharm it runs correctly and does its designated job, but when trying to run it through a batch file (i need thath to do some automation later on) it returns an empty csv file instead of the one it's supposed to.
The batch file is created by writing in a .txt file:
"path of python.exe" "path of the .py file of the script"
and then changing the .txt extension to a .bat one (that's the process i found online about creating batch files) and the script is:
import pandas as pd
import glob
import os
path = r'Path for origin files.'
r_path = r'Path of resulting file'
if os.path.exists(r_path + 'csv'):
os.remove(r_path + 'csv')
if os.path.exists(r_path + 'txt'):
os.remove(r_path + 'txt')
files = glob.glob(path)
column_list = [None] * 44
for i in range(44):
column_list[i] = str(i + 1)
df = pd.DataFrame(columns = column_list)
for name in files:
df_n = pd.read_csv(name, names = column_list)
df = df.append(df_n.iloc[-1], ignore_index=True)
del df_n
df.to_csv(r_path, index=False, header=False)
del df
What am i doing wrong?
Pycharm automatically adds an environment variable called PYTHONPATH to the command before it executes it. The PYTHONPATH variable indicates the python process what the base path is for the execution of the script.
For example if your file path is awesomecsv.csv how should the python process know which folder it should look for to find that file?
PYTHONPATH=/my/path/tothefolderwheremyscriptis/ python my_script.py
above with the PYTHONPATH you tell python what folder you are executing your python command from.
related documentation
Probably the error is in the paths of the csv files, Pycharm probably is setting for you some kind of workspace folder try to use a full path to the directory

How to open a specific path with open()?

I'm trying to build a file transfer system with python3 sockets. I have the connection and sending down but my issue right now is that the file being sent has to be in the same directory as the program, and when you receive the file, it just puts the file into the same directory as the program. How can I get a user to input the location of the file to be sent and select the location of the file to be sent to?
I assume you're opening files with:
open("filename","r")
If you do not provide an absolute path, the open function will always default to a relative path. So, if I wanted to open a file such as /mnt/storage/dnd/5th_edition.txt, I would have to use:
open("/mnt/storage/dnd/4p5_edition","r")
And if I wanted to copy this file to /mnt/storage/trash/ I would have to use the absolute path as well:
open("/mnt/storage/trash/4p5_edition","w")
If instead, I decided to use this:
open("mnt/storage/trash/4p5_edition","w")
Then I would get an IOError if there wasn't a directory named mnt with the directories storage/trash in my present folder. If those folders did exist in my present folder, then it would end up in /whatever/the/path/is/to/my/current/directory/mnt/storage/trash/4p5_edition, rather than /mnt/storage/trash/4p5_edition.
since you said that the file will be placed in the same path where the program is, the following code might work
import os
filename = "name.txt"
f = open(os.path.join(os.path.dirname(__file__),filename))
Its pretty simple just get the path from user
subpath = raw_input("File path = ")
print subpath
file=open(subpath+str(file_name),'w+')
file.write(content)
file.close()
I think thats all you need let me know if you need something else.
like you say, the file should be in the same folder of the project so you have to replace it, or to define a function that return the right file path into your open() function, It's a way that you can use to reduce the time of searching a solution to your problem brother.
It should be something like :
import os
filename = "the_full_path_of_the_fil/name.txt"
f = open(os.path.join(os.path.dirname(__file__),filename))
then you can use the value of the f variable as a path to the directory of where the file is in.

Python import module load_source

I have question on the load_source.
when my 2 .py files are in the same directory /home/pi they work fine.
main.py
#!/usr/bin/python
import buttonlog
buttonlog.py
import datetime
i = datetime.datetime.now()
#OPEN FILE & APPEND
f=open('buttonlog.txt','a')
#WRITE DATE THEN NEW LINE WITH THE '\N'
f.write(i.isoformat() + '\n')
When I run python main.py it writes an entry like I'd expect.
However I'd like to store main.py in another directory so I tried this, it is stored in the /home/pi/test
#!/usr/bin/python
import imp
imp.load_source('buttonlog', '/home/pi/buttonlog.py')
When I run python /home/pi/test/main.py I do not get any errors nor does it write an entry into my file. What am I doing wrong?
The secret is the use of the open command.
As the documentation says about the first argument,
file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped.
By passing just "buttonlog.txt", this is not an absolute pathname, so it's relative to the current working directory.
The simplest way to fix this is to use a full path. If you always want it writing in to /home/pi, you just need:
f=open('/home/pi/buttonlog.txt','a')
There are other alternatives, though I think this is the cleanest. You could also change your current working directory prior to issuing the open command for the same results. Simply put this code above the open line:
import os
os.chdir("/home/pi")

Get script's location when called by another app?

How can my Python script reliably get its own location so it can write a file next to itself?
I made a Python script that writes a new file with some working statistics:
import os
NAME = __file__.split('/')[-1]
PATH = os.path.dirname(os.path.realpath(__file__))
PROPER_PATH = os.path.join(MY_PATH, MY_NAME)
with open(os.path.join(PATH, STATS_NAME), 'wb') as statsfile:
statsfile.write(mystats)
This works great and I can call it from anywhere and it writes the stats file in the same place, next to the script. Apart from when I call the script from a macro VBA script.
The script gets called okay but writes its data to:
"C:\Users\lsp\AppData\Roaming\Microsoft\Templates"
How do I make sure it writes to the correct path (same as the script path)?
As a work-around I can imagine giving the path as an argument and then providing it in the VBA but surely there's a Pythonic way to achieve the behavior?

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

How can I set the current path of my python file "myproject.py" to the file itself?
I do not want something like this:
path = "the path of myproject.py"
In mathematica I can set:
SetDirectory[NotebookDirectory[]]
The advantage with the code in Mathematica is that if I change the path of my Mathematica file, for example if I give it to someone else or I put it in another folder, I do not need to do anything extra. Each time Mathematica automatically set the directory to the current folder.
I want something similar to this in Python.
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 ?)
I want to set the directory in which the python file is, as working directory
There are two step:
Find out path to the python file
Set its parent directory as the working directory
The 2nd is simple:
import os
os.chdir(module_dir) # set working directory
The 1st might be complex if you want to support a general case (python file that is run as a script directly, python file that is imported in another module, python file that is symlinked, etc). Here's one possible solution:
import inspect
import os
module_path = inspect.getfile(inspect.currentframe())
module_dir = os.path.realpath(os.path.dirname(module_path))
Use the os.getcwd() function from the built in os module also there's os.getcwdu() which returns a unicode object of the current working directory
Example usage:
import os
path = os.getcwd()
print path
#C:\Users\KDawG\Desktop\Python

Categories