I wanted to play a .wav file, without using external modules, and i read i could do that using this:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", /Users/me/Downloads/sample.wav])
I however get:
SyntaxError: invalid syntax
If i use os.path.realpath to get the absolute path of the file, i get just the same thing. (The path i see at get info)
Environment is OSX, Python 2.7
Can someone tell me what i am doing wrong? I am new to Python (and to Programming).
There are multiple problems.
Indentation
Code inside the function should be indented, to show that it is part of the function
File name should be in a quotes
It should be a string
It should be:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", "/Users/me/Downloads/sample.wav"])
Related
Preface: I am a beginner to Python
Problem statement: I am writing a script wherein I will be launching an application (Gotit.exe) sitting at particular path lets say D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe. I have kept the python-script also in myPythonFolder.
I am accessing the folder path via os.path.dirname(os.path.realpath(__file__)) and selecting particular application by appending it with \Gotit.exe but when passing the same appended string stored in a variable i.e. GotitexePath to os.system(GotitexePath) its throwing error as,
'D:\Some ' is not recognized as an internal or external command,
operable program or batch file.**
Kindly help me out to solve the said issue
I am using python 3.8.2 on Win10 Machine
The error is pointing to Some Folder name. Since there is a space in path you provide, the system doesn't know whether it is a part of folder name or it is a next argument to the command.
You need to escape the blank space. There are multiple ways to to it. For example wrap the path with double quotes:
"D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe"
For more ways see this post
os.system("\"%s\"" % GotitexePath)
A the previous replies say, you need to add additional quotation marks around the path for the windows command line.
I need to extract text from a PDF. I tried the PyPDF2, but the textExtract method returned an encrypted text, even though the pdf is not encrypted acoording to the isEncrypted method.
So I moved on to trying accessing a program that does the job from the command prompt, so I could call it from python with the subprocess module. I found this program called textExtract, which did the job I wanted with the following command line on cmd:
"textextract.exe" "download.pdf" /to "download.txt"
However, when I tried running it with subprocess I couldn't get a 0 return code.
Here is the code I tried:
textextract = shlex.split(r'"textextract.exe" "download.pdf" /to "download.txt"')
subprocess.run(textextract)
I already tried it with shell=True, but it didn't work.
Can anyone help me?
I was able to get the following script to work from the command line after installing the PDF2Text Pilot application you're trying to use:
import shlex
import subprocess
args = shlex.split(r'"textextract.exe" "download.pdf" /to "download.txt"')
print('args:', args)
subprocess.run(args)
Sample screen output of running it from a command line session:
> C:\Python3\python run-textextract.py
args: ['textextract.exe', 'download.pdf', '/to', 'download.txt']
Progress:
Text from "download.pdf" has been successfully extracted...
Text extraction has been completed!
The above output was generated using Python 3.7.0.
I don't know if your use of spyder on anaconda affects things or not since I'm not familiar with it/them. If you continue to have problems with this, then, if it's possible, I suggest you see if you can get things working directly—i.e. running the the Python interpreter on the script manually from the command line similar to what's shown above. If that works, but using spyder doesn't, then you'll at least know the cause of the problem.
There's no need to build a string of quoted strings and then parse that back out to a list of strings. Just create a list and pass that:
command=["textextract.exe", "download.pdf", "/to", "download.txt"]
subprocess.run(command)
All that shlex.split is doing is creating a list by removing all of the quotes you had to add when creating the string in the first place. That's an extra step that provides no value over just creating the list yourself.
I'm trying to execute a visual basic script that is located in my personal macro workbook on an excel file that I'm creating. Here's what I have so far:
import os
import win32com.client
df2.to_excel("Apartments.xlsx")
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.open(filename="C:\Users\my\full\path\Apartments.xlsx", ReadOnly=1)
xl.Application.Run("Apartments.xlsx!create_chart.create_chart_proc")
It's throwing an error when opening the excel file on line 5, I have a feeling line 6 won't work either because it comes from my personal macro book. Anyone have ideas on how to get it to function?
PS. my module name is "create_chart" and my macro name is "create_chart_proc"
It would help if you said what error you are getting, and I know that this path
"C:\Users\my\full\path\Apartments.xlsx"
is not really what is in your code. But \ inside a string has a special meaning in Python (for example \n is a newline and \f is a formfeed). Whether your \ is interpreted as you intend depends on the character that comes after it. When you use Windows paths in Python it's generally less error-prone to use a raw string.
r"C:\Users\my\full\path\Apartments.xlsx"
When you get that line to work, if the next line gives trouble, then post that as a separate question.
I got the first part of the code to work simply running the code as follows:
xl.workbooks.open("C:\Users\my\full\path\Apartments.xlsx")
Posted a separate question to get the macro to work correctly.
I am working on a python script that installs an 802.1x certificate on a Windows 8.1 machine. This script works fine on Windows 8 and Windows XP (haven't tried it on other machines).
I have isolated the issue. It has to do with clearing out the folder
"C:\Windows\system32\config\systemprofile\AppData\LocalLow\Microsoft\CryptURLCache\Content"
The problem is that I am using the module os and the command listdir on this folder to delete each file in it. However, listdir errors, saying the folder does not exist, when it does indeed exist.
The issue seems to be that os.listdir cannot see the LocalLow folder. If I make a two line script:
import os
os.listdir("C:\Windows\System32\config\systemprofile\AppData")
It shows the following result:
['Local', 'Roaming']
As you can see, LocalLow is missing.
I thought it might be a permissions issue, but I am having serious trouble figuring out what a next step might be. I am running the process as an administrator from the command line, and it simply doesn't see the folder.
Thanks in advance!
Edit: changing the string to r"C:\Windows\System32\config\systemprofile\AppData", "C:\Windows\System32\config\systemprofile\AppData", or C:/Windows/System32/config/systemprofile/AppData" all produce identical results
Edit: Another unusual wrinkle in this issue: If I manually create a new directory in that location I am unable to see it through os.listdir either. In addition, I cannot browse to the LocalLow or my New Folder through the "Save As.." command in Notepad++
I'm starting to think this is a bug in Windows 8.1 preview.
I encountered this issue recently.
I found it's caused by Windows file system redirector
and you can check out following python snippet
import ctypes
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
#Example usage
import os
path = 'C:\\Windows\\System32\\config\\systemprofile\\AppData'
print os.listdir(path)
with disable_file_system_redirection():
print os.listdir(path)
print os.listdir(path)
ref : http://code.activestate.com/recipes/578035-disable-file-system-redirector/
You must have escape sequences in your path. You should use a raw string for file/directory paths:
# By putting the 'r' at the start, I make this string a raw string
# Raw strings do not process escape sequences
r"C:\path\to\file"
or put the slashes the other way:
"C:/path/to/file"
or escape the slashes:
# You probably won't want this method because it makes your paths huge
# I just listed it because it *does* work
"C:\\path\\to\\file"
I'm curious as to how you are able to list the contents with those two lines. You are using escape sequences \W, \S, \c, \s, \A in your code. Try escaping the back slash like this:
import os
os.listdir('C:\\Windows\\System32\\config\\systemprofile\\AppData')
I need to enter the contents of a text (.txt) file as input for a Python (.py) file. Assuming the name of the text file is TextFile and the name of the Python file PythonFile, then the code should be as follows:
python PythonFile.py < TextFile.txt
Yet, when I try to do this in IDLE and type in
import PythonFile < TextFile,
IDLE gives me an invalid syntax message, pointing to the < sign. I tried all sorts of variations on this theme (i.e.,using or not using the file name extensions), but still got the same invalid-syntax message. How is the syntax different for input redirection in IDLE?
If it works in the command line, then why do you want to do this in IDLE? There are ways to achieve a similar result using, for example, subprocess, but a better way would be to refactor PythonFile.py so that you can call a function from it, e.g.:
>>> import PythonFile
>>> PythonFile.run_with_input('TextFile.txt')
If you post the contents of PythonFile.py, we might be able to help you do this.