python write-to-file issue, cannot understand behavior - python-2.7

Env: Python 2.7
I write some code that write list terms into text files.
Windows sublime remote connected to ubuntu server to edit files
Behavior:
I use file cmd to see the property of the text file, find something interesting.
I guess it could be "xx.txt UTF-8 Unicode text",
but actually it is "xx.txt: DOS executable (COM)"
By mistake I use with clause and f.close() method at the same time. I cannot understand behavior that happens.
Code:
with open(output, 'w') as f:
for comment in sample_list:
f.write(comment + '\n')
f.close()

Related

Sublime Text 3 files not opening in command line/powershell

I’ve just finished Codecademy and am setting up Python (2.7) and Sublime Text 3 on my own computer using the Codecademy guide page. I can successfully perform simple operations (eg. Print “Hello world”) a) in Python and b) by using ctrl+b on text in my Sublime Text 3 editor page, which gives the output in the box at the bottom of the page.
I’m stuck/confused when it comes to running the Sublime Text 3 file through the command line and confused on the powershell vs. command line issue (I’m running Windows 10).
My command line does not display the $ sign in the codecademy page example (https://www.codecademy.com/articles/setup-python), so should I be entering the commands below through Powershell rather than the command line? If so, I don’t get the $ in Powershell either.
If I carry on regardless and try to change directories etc through Command Prompt or Powershell, I only get error messages and can't seem to run the Sublime Text 3 file.
I’m also not clear on whether this is an issue about which directory I’m in. Running dir in both the Command prompt and Powershell returns the result: after I’ve opened Python, but lists all subfolders of C:\Users\my_name if I run it before opening Python. Does this mean that I need to save my Sublime Text files in some sort of subfolder of Python in order to be able to run them as above?
Or does it not matter that I’m not able to run the Sublime Text 3 files through Python directly and I should just stick to doing so through Sublime Text 3 itself? Will this limit me later on?
Thanks for your help
BC89

python error can't open pdf in application from python

I am using windows 10 and python 2.7.13 .
My target is to open a pdf in acrobat reader at a specific page number.
I am using the code I received from one of the questions in the forum here.
import subprocess
import os
path_to_pdf = os.path.abspath('C:\test_file.pdf')
# I am testing this on my Windows Install machine
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe')
# this will open your document on page 12
process = subprocess.Popen([path_to_acrobat, '/A', 'page=12', path_to_pdf], shell=False, stdout=subprocess.PIPE)
process.wait()
It opens up the acrobat reader application but the file doesnt open and i get the error :"There was an error opening this document. The filename, directory name, or volume label syntax is incorrect
but when I use these commands in cmd without python I am successfully able to open the pdf without any error. Please help.
Try to open the file with r:
path_to_pdf = os.path.abspath(r'C:\test_file.pdf')
path_to_acrobat = os.path.abspath(r'C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe')
Check this out too, it will show to you an example of an error with the white spaces on the path
Please check the path of your acrobat reader.
In my case:
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\AcroRd32.exe')

subprocess.Popen unexpected behavior

I inherited some Python code which uses subprocess.Popen to print a pdf file using Adobe Reader. The code has worked well for years running on Windows 7, I am getting it set up on Windows 10. For some reason the call to Popen starts an Adobe Reader process, but does not open the application. The code works as expected if I run it from the Python interpreter. But when it when it is running as a Windows process, Adobe Reader does not open, or print the pdf. I checked the command line parameters for the running process and it is invoked exactly the same as when run from the Python Interpreter. A code snippet is below.
printFileName = r'"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe" /p /h /t '
printFileName = printFileName + filename
subprocess.Popen(printFileName)
filename is the full path to a .pdf file.
I'm not really sure where to even begin figuring this out so any pointers or suggestions on how to improve the question would be appreciated.

Python subprocess is unstable on Windows 10?

p = subprocess.Popen([executable_file])
This is only code that I am using to run the python subprocess. However, it has unknown issue that cause my program cannot open the executable file as expected.
executable_file is one file link (PATH) that locate executable program.
Ex. C:\Users\SharkIng\Dev\WhatEverSystem\Builds\Windows\program-123456789-123456789.exe
The python subprocess.Popen should run the program. However, sometime it works and sometime it is not (I did not change any code between this two situation)
OS: Windows 10
Python: 2.7.*
Error: [Error 2] The system cannot find the file specified
BUT: It is working if you manually run subprocess.Popen([executable_file_path_string]) (with same file and path) it work.
WHY this happen?? Is that because some problem with Windows Python? or it is because my setting mess me up?
UPDATE: It is not some reason such as NOT FULL PATH. I can have it working with exact same code. If I ran same code in a python shell, by typing each line of code. It works. But if I put exact same code in a .py file and run it with python file.py, it showing the error
UPDATE 2: Another team member have same error with Windows 7 and Python 2.7.* This code used to work this morning. AGAIN I didn't change anything. That is way I am asking if it is unstable.
There is no problem with subprocess. If you want to run a specified executable, you need to give the full path to that file. The most likely reason you're seeing variable behavior is that sometimes you're in the directory with the .exe, and other times you're not.

Trouble with clang_complete, no error and loaded but does not work

I am trying to use clang_complete for vim on Arch Linux. I built vim and vimball plugin from source. I see +python/dyn and +python3/dyn in vim --version output. I use the following .vimrc file :
syntax on
filetype plugin on
let g:clang_complete_copen=1
let g:clang_complete_auto=0
let g:clang_user_options='|| exit 0'
let g:clang_use_library=1
let g:clang_library_path='/usr/lib/libclang.so'
In :scriptnames, I see ~/.vimrc/plugin/clang_complete.vim is loaded.
Overall, it does not work! If I open a C++ file then vim shows incorrect UI behaviour without reporting any error. e.g. cursor is invisible, typing words takes place in wrong positions, no syntax highlight, ... . The cursor remains invisible even after quit.
I think clang library can be loaded since any change in the path makes an error.
Is there any mistake in the above settings?