Hey guys I have a pretty simple code.
subprocess.call(dirIFLO+'IFLO.exe')
Where dirIFLO is just a folder above the current working directory
This code is suppose to run IFLO.exe while my code waits. The program does start but somehow it does not manage to get the files they were suppose to in the same directory it is. If I manually open it I get it working just fine.
I have managed to make it work by accessing the subdirectory and calling the program instead of opening the subroutine straight from the working directory.
w = os.getcwd()
os.chdir(dirIFLO[:-2])
subprocess.call('IFLO.exe')
os.chdir(w)
Related
I am creating a program in Python that requires the user to place images into an Input folder, and then take images out of an Output folder. As this will become an application, the Input and Output folders will be very difficult to navigate to, being buried in the app's contents.
I am looking for a way to open folders onscreen so that a user can add or remove their own files from these folders, without knowing the exact location of the folders they are interacting with.
I am thoroughly stumped on this problem, and I appreciate all of your time.
Thank you very much.
*Edit: I am working on MacOS
If the program is intended for use on windows, it seems like you could use the solution here, where you could open explorer as a sub process and then open the path to the file.
For example
import subprocess
subprocess.Popen(r'explorer /select, FilePath')
You could also use os, and os.startfile(FilePath).
I'm using Geany as my editor and when I first started using it, writing to files worked fine but somehow it randomly stopped working. The code executes without any errors but the file isn't created / is empty if already created and I've no idea why.
Simple code as below doesn't work:
filename = 'dogs'
with open(filename, 'w') as f:
f.write('tester')
with open(filename, 'r') as f:
contents = f.read()
print(contents)
The output I get from the 'read' method looks correct on the console output (it just prints 'tester'), but no file is created / edited in my directory.
Geany also has a weirdly complex debugger (if anyone has any helpful guides on how to use it please let me know) so I can't debug properly. I've tried all that I know including using an absolute file path, running in admin mode. The issue is also present when I try to use Pygal to render_to_file(), which is the project I'm working on so right now I can't go any further because anything that requires writing doesn't work. FYI it reads fine.... It's like Geany doesn't have admin rights or something?
EDIT: I've run this code on a python shell (without a .py file) and it worked fine, creating the file as desired. I then ran it using CMD with the .py file and it didn't work. Also ran using Pycharm, it doesn't work when I run it normal but it works when I run it in debug mode? It doesn't seem to be a Geany specific issue, but I am so confused!
Because the code works in the shell I strongly suspect the file is being created somewhere but you are looking in the wrong place.
I know you mentioned absolute paths but I just want to reiterate that you are not currently using an absolute path. An absolute path must start with a '/' (linux/mac) or something like: 'C:/' (windows).
Here are 2 things to try:
1)
Change the name 'dogs' to something really obscure and then do a global search on your whole hard disk for that name. You'll probably find it.
2) Get python to tell you where the file is like this:
import os
filename = 'dogs'
with open(filename, 'w') as f:
f.write('tester')
print(os.path.realpath(f.name))
Got the solution - the files were being created the whole time every time I ran my code, however my antivirus software Comodo contained them within a hidden folder in my drive which couldn't be found by a normal search.
I'm not sure if this a common problem with AV software or just Comodo, can't find anything on the net about it but there you go. I removed these files and the programs from its radar and it now works perfectly.
Although one mystery is how the python shell bypassed that problem. That threw me off thinking it was AV.
Just a brief outline of what I'm doing: I'm trying to automate some pdf merging routine with python in a network directory, which involves copying, deleting and creating files at a specific network location. Apologies if my language is not very precise.
I'm coding on windows 7, using python 3.6. The program will need to be distributed on other machines, so local and temporary fixes will probably not help. The code I wrote is fully functional and works fine with all the local folders and files, however, now that I need to make use of it on the network, I am having some difficulties accessing the folder I need.
Here is what I have tried:
os.system("pushd " + "\\" + "\\netWorkDrive\Reports")
check_output("pushd " + "\\" + "\\netWorkDrive\Reports", shell=True)
pushd and popd work fine when entered just in the cmd, but when I do system calls through python, they just don't go through. I send a system call, and it runs correctly, but then when I "cd" a current directory, it shows that I'm still in my previous one. If done through the cmd manually, everything works as desired. I have googled the issue, but did not end up finding anything working/useful. I would really appreciate any suggestions, and let me know if I need to clarify my problem further.
Thank you!
I would not use pushd/popd in such a way, I would just include the full paths, including network paths in the paths of whatever file operation I need to do
However if I really need to change working directory, I would do this with python:
import os
original_working_directory = os.getcwd()
# do stuff
new_networked_directory = r'\\server\share\folder'
# change to the networked directory
os.chdir(new_networked_directory)
# do stuff
#changeback to original working directory
os.chdir(original_working_directory)
# do more stuff
There should be no need for "temp drives" or the like really.
I am working on a python script that will walk through a large collection of .py files and write out individual .bat files that can be called upon to run these scripts.
I understand typical python output
directory = 'c:/'
OPATH = open(str(directory) + 'output_file.txt', 'w')
However if I try to do output_file.bat I receive an error, and it won't let me write out to it.
What I would like written in the batch files.
Creating a BAT file for python script
Is there any documentation on how to write out other kinds of files with python? I would also be interested in having a python script generate .c files as well.
Try this, using os.path.join(). Your error was merely trivial, don't worry.
import os
directory = 'C:/'
with open(os.path.join(directory, 'output_file.bat'), 'w') as OPATH:
OPATH.writelines(['#echo off',
'c:\python27\python.exe c:\somescript.py %*',
'pause'])
This provides a cross-platform solution to your problem. Although your error was due to a missing /, you should not hardcode this. This is the best way to join two paths and thus solve your problem.
This is my first question.
My python script opens and reads from a present text file using the following simple funct:
open("config.ini", "r")
As this is a relative path it is supposed to work because config.ini is placed in the same directory like the script is when it is launched, that should be the current working dir.
In fact this works perfectly on all of my 3 linux boxes, but I have one user who demands support because he gets an error while opening config.ini. The error raises because
os.path.exists("config.ini")
returns false even if the file is there!
Trying to fix this problem we found out that the only way to make it work is to place config.ini in his home directory despite the supposed working directory is another.
Also, if my script tries to create a file in the present working directory, the file is always created in his home dir instead, and so I think that for some reason his working dir is always home!
How can I troubleshoot this problem? Maybe I could introduce absolute paths, but I am afraid that os.getcwd() would return the homedir instead of the correct one.
Should I maybe suggest this user to fix his machine in some way?
Sorry for this long question but english is not my first language and I am a beginner in coding, so have some difficulties to explain.
Thank you very much in advance! =)
Could it be that the user is executing your script from his home directory?
I.e. suppose the script is in:
/home/user/test/foo/foo.py
But the user calls it thus:
/home/user> python test/foo/foo.py
In this case, the "current directory" the script sees is /home/user.
What you can do is find out the directory the script itself resides in by calling this function:
import os
def script_dir():
return os.path.dirname(os.path.realpath(__file__))
It will always return the directory in which the script lives, not the current directory which may be different. You can then store your configuration file there safely.
Along the same lines as Eli Bendersky's suggestion, you might want to try:
os.path.exists(os.path.join(sys.path[0],"config.ini"))
since sys.path[0] should always be the directory in which the script resides.