Python on windows removes created file - windows

I'm trying to write a file with a python program. When I perform all the actions command line, they all work fine. The file is created.
When I perform the actions in a python script, the file does not exist after the script terminates.
I created a small script that demonstrates the behavior.
import os
import os.path
current_dir = os.getcwd()
output_file = os.path.join(current_dir, "locations.js")
print output_file
f = open(output_file, "w")
f.write("var locations = [")
f.write("{lat: 55.978467, lng: 9.863467}")
f.write("]")
f.close()
if os.path.isfile(output_file):
print output_file + " exists"
exit()
Running the script from the command line, I get these results:
D:\Temp\GeoMap>python test.py
D:\Temp\GeoMap\locations.js
D:\Temp\GeoMap\locations.js exists
D:\Temp\GeoMap>dir locations.js
Volume in drive D is Data
Volume Serial Number is 0EBF-9720
Directory of D:\Temp\GeoMap
File Not Found
D:\Temp\GeoMap>
Hence the file is actually created, but removed when the script terminates.
What do I need to do the keep the file?

Problem was solved by changing firewall settings.

Related

Issues with os.listdir when script is an executable

I have created a script that takes a file from one folder and produces another file in another folder. This is a project to convert one format into another to be used by people who dont have strong background in informatics so I have created a folder with the script plus the input folder and the output folder. The user just need to put the input file in the input folder and take the results from the output folder.
The script works fine if I run this python script when running with visual code as well as If I run the script using the terminal ( python CSVtoVCFv3.py )
but when I convert my script in an executable with pyinstaller I found the next error.
File "CSVtoVCFv3.py", line 99, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/manoldominguez/input/'
[99327] Failed to execute script CSVtoVCFv3
The code used in line 99 is:
97 actual_path = os.getcwd()
98 folder_input = '/input/'
99 input_file_name = os.listdir(actual_path+folder_input)
100 input_file_name= ''.join(input_file_name)
101 CSV_input = actual_path+folder_input+input_file_name
I have also tried this:
actual_path = (os.path.dirname(os.path.realpath('CSVtoVCFv3.py')))
So as conclusion as far as I can understand the issue is:
In these lines If I run my script I get this
'/Users/manoldominguez/Desktop/CSVtoVCF/input/'
If my script is ran with my executable I get this
'/Users/manoldominguez/input/'
os.getcwd() gives Current Working Directory - it means folder in which script was executed, but it doesn't have to be folder in which script is saved. This way you can run code in different folder and it works with files in different folder - and it can be usefull.
But if you need with files in folder where you have script then you can get this folder using
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
or
import sys
SCRIPT_PATH = os.path.dirname(os.path.realpath(sys.argv[0]))
not with 'CSVtoVCFv3.py'
And then you can join it
SCRIPT_PATH = os.path.dirname(os.path.realpath(sys.argv[0]))
folder_input = '/input/'
full_folder_input = os.path.join(SCRIPT_PATH, folder_input)
all_filenames = os.listdir(full_folder_input)
for input_file_name in all_filenames:
#CSV_input = os.path.join(full_folder_input, input_file_name)
CSV_input = os.path.join(SCRIPT_PATH, folder_input, input_file_name)
I only don't like your
input_file_name = os.listdir(actual_path+folder_input)
input_file_name= ''.join(input_file_name)
because listdir() may gives more files and then your join may create incorrect path. Better get input_file_name[0] for single file or use for-loop to work with all files in folder.
BTW: Maybe you should use sys.argv to get path as parameter and then everyone may decide where to put file.

Using ls to launch a Python script on each subdirectory

I created this little Python script Essai_Bash.py to make some tests:
#!/usr/bin/python
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument('-i', action='store', dest='InputDir', help='Working Directory') # Empty folders for outputs.
parser.add_argument('--version', action='version', version='%(prog)s 0.1')
results = parser.parse_args()
print 'Current input directory =', results.InputDir
dir_path=str(os.path.abspath(results.InputDir)) # Retrieving an output folder name to use as species ID:
path,CodeSp = os.path.split(dir_path)
print "Currently working on species: "+str(CodeSp)
Back to my shell, I type the following command, expecting my script to run on each directory that is present in my "Essai_Bash" folder:
listdir='ls ../Main_folder/' # I first used backtips instead of simple quotes but it did not work.
for dir in $listdir; do ./Essai_Bash.py -i ../Main_folder/$dir; done
I am surely missing something obvious but it does not work. It seems like $listdir is considered as a characters strings and not a list of directories. However, just typing $listdir in my shell actually gives me this list!
Just use glob extension, parsing ls output is not safe.
Also dir variable already contains ../Main_folder/
listdir=( ../Main_folder/*/ )
for dir in "${listdir[#]}"; do ./Essai_Bash.py -i "$dir"; done

Accessing files within a parent folder and it's sub-folders using Python

I have a folder which has subfolders containing 1000s of DICOM images which I want to read in from IDLE and analyze.
I have used the following code to find file paths:
import sys
print sys.path
I subsequently tried placing my folder which I want to access in these file paths, however I still can not access the files and I get the following error:
>>> fp = open(fp, 'rb')
IOError: [Errno 2] No such file or directory: 'IM-0268-0001.dcm'
I have also tried:
sys.path.insert(0, 'C:/desktop/James_Phantom_CT_Dec_16th/Images')
But this did not work for me either. Help much appreciated, very frustrated.
(using Python 2.7, 64 bit windows OS).
When opening a file, Python does not search the path. You must specify the full path to open:
d = 'C:/desktop/James_Phantom_CT_Dec_16th/Images'
fp = open(d +'IM-0268-0001.dcm',"rb")
Edit: d is the string that will hold the path so that you don't have to re-type it for each file. fp will hold the file object that you will work with. The "rb" is the way you want to open the file:
r - read
w - write with truncate
a - append
r+ - read and write
Also, if working in windows, add "b" to work with binary files. See here.

Mass Convert .xls and .xlsx to .txt (Tab Delimited) on a Mac

I have about 150 .xls and .xlsx files that I need converting into tab-delimited. I tried using automator, but I was only able to do it one-by-one. It's definitely faster than opening up each one individually, though. I have very little scripting knowledge, so I would appreciate a way to do this as painlessly as possible.
If you would be prepared to use Python for this I have written a script that converts Excel spreadsheets to csv files. The code is available in Pastebin.
You would just need to change the following line:
writer = csv.writer(fileout)
to:
writer = csv.writer(fileout, delimiter="\t")
to make the output file tab delimited rather than the standard comma delimited.
As it stands this script prompts you for files one at a time (allows you to select from a dialogue), but it could easily be adapted to pick up all of the Excel files in a given directory tree or where the names match a given pattern.
If you give this a try with an individual file first and let me know how you get on, I can help with the changes to automate the rest if you like.
UPDATE
Here is a wrapper script you could use:
#!/usr/bin/python
import os, sys, traceback
sys.path.insert(0,os.getenv('py'))
import excel_to_csv
def main():
# drop out if no arg for excel dir
if len(sys.argv) < 2:
print 'Usage: Python xl_csv_wrapper <path_to_excel_files>'
sys.exit(1)
else:
xl_path = sys.argv[1]
xl_files = os.listdir(xl_path)
valid_ext = ['.xls', '.xlsx', '.xlsm']
# loop through files in path
for f in xl_files:
f_name, ext = os.path.splitext(f)
if ext.lower() in valid_ext:
try:
print 'arg1:', os.path.join(xl_path,f)
print 'arg2:', os.path.join(xl_path,f_name+'.csv')
excel_to_csv.xl_to_csv(os.path.join(xl_path,f),
os.path.join(xl_path,f_name+'.csv'))
except:
print '** Failed to convert file:', f, '**'
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
for line in lines:
print '!!', line
else:
print 'Sucessfully conveted', f, 'to .csv'
if __name__ == '__main__':
main()
You will need to replace the :
sys.path.insert(0,os.getenv('py'))
At the top with an absolute path to the excel_to_csv script or an environment variable on your system.
Use VBA in a control workbook to loop through the source workbooks in a specified directory or a list of workbooks, opening each, saving out the converted data, then closing each in turn.

Opening Blender (a program) from a specific filepath, relative paths, Unix executable

In my previous question, Open a file from a specific program from python, I found out how to use subprocess in order to open a program (Blender) — well, a specific .blend file — from a specific file path with this code.
import os
import subprocess
path = os.getcwd()
os.system("cd path/")
subprocess.check_call(["open", "-a", os.path.join(path, "blender.app"),"Import_mhx.blend"])
With the help of a guy at a forum, I wanted to use relative paths inside the .blend file, so I changed the code in this way (for Windows)
import os
import subprocess
# This should be the full path to your Blender executable.
blenderPath = "/cygdrive/c/Program Files/Blender Foundation/blender-2.62-release-windows32/blender.exe"
# This is the directory that you want to be your "current" directory when Blender starts
path1 = "/Users/user/Desktop/scenario/Blender"
# This makes makes it so your script is currently based at "path1"
os.chdir(path1)
subprocess.check_call([blenderPath, "Import_mhx.blend"])
and for Mac,
import os
import subprocess
path = os.getcwd()
os.system("cd path/")
print (path)
# This should be the full path to your Blender executable.
blenderPath = path + "/blender.app/Contents/macos/blender"
# This is the directory that you want to be your "current" directory when Blender starts
path1 = "/Users/user/Desktop/scenario/Blender"
# This makes makes it so your script is currently based at "path1"
os.chdir(path1)
subprocess.check_call([blenderPath, "Import_mhx.blend"])
Results:
In Windows, it works fine.
On Macs, the result is that the file is opened, but the program seems not to be opened. It is quite strange, I think.
Questions:
Is there any extension that I should place for the blender (UNIX executable file) in order for it to open?
Is there any other way that I can do it in order to open the program correctly, but also be able to use relative paths inside .blend files?
import os
import subprocess
blenderPath = "./blender.app/Contents/MacOS/blender"
path1 = "./"
os.chdir(path1)
subprocess.check_call([ blenderPath, "Animation.blend"])
Both blender opens perfectly and relative paths inside .blend file work ok:)

Resources