This question already has answers here:
How to compile python script to binary executable
(4 answers)
Closed 6 years ago.
if i create python code with .py and i want my friends get the code and it will work on theme computer (open files and print them).
how can i do that without install.
i mean i dont wont them yto install python.
i can do it as .exe or something?
thanks!
my code is stupid but required here so -
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()
I would use Py2exe
but there are more solutions here are two links
link 1
link 2
Related
This question already has answers here:
How can I create files on Windows with embedded slashes, using Python?
(3 answers)
Closed 2 years ago.
When I run the following code, instead of creating a text file in my working directory with the name '03/08/2020.txt', it generates an error FileNotFoundError: [Errno 2] No such file or directory: '03/08/2020.txt'. As far as I think, it is just because of the slashes.
But anyhow, I want to create a text file with slashes because this thing is a part of a large code and I have to work with dates (for attendance purposes).
dates = ['03/08/2020', '1', '2', '3']
def test(alist):
myfile = open(alist[0])+'.txt', 'w')
for i in alist:
myfile.write(f"{i}\n")
myfile.close()
test(dates)
is there a way yo handle this issue?
As jdaz said, you can instead use "03-08-2020.txt"
This is because on windows, you can't add to the following characters to your file names:
\ / : * ? " < > |
If you try to rename a file with one of those characters, you'll see a message saying you can't do so.
This question already has answers here:
Output a python script to text file
(4 answers)
Closed 5 years ago.
i have the following script in python with a while loop
from time import sleep
while True:
print "hola"
print "mundo"
sleep(2)
and i want to write the output to a file with the following code:
import subprocess
with open("output.log", "w") as output:
subprocess.call(["python", "./main.py"], stdout=output);
the thing is that the while never ends, the file output.log never gets the output from the script, i wonder if there is a way to do it.
You can simply do it by the following command.
python filename.py > output.log
The above command works for both linux and windows.
This question already has answers here:
Understanding python subprocess.check_output's first argument and shell=True [duplicate]
(2 answers)
Closed 6 years ago.
I'm trying to run this code that should open the selected mp3 with VLC, use Line 1 as the audio output, and close when it's done. But the arguments don't all seem to be getting through.
Python code
import subprocess
vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe"
audiopath = "C:\\Users\\Aidan\\Desktop\\test.mp3"
args = [vlcpath, audiopath, "--aout=waveout", '--waveout-audio-device="Line 1 (Virtual Audio Cable) ($1,$64)"', "--play-and-exit"]
subprocess.call(args, shell=True)
for i in args: #for diagnostic purposes
print(i)
Which should run similarly to this command line input
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" C:\Users\Aidan\Desktop\test.mp3 --aout=waveout --waveout-audio-device="Line 1 (Virtual Audio Cable) ($1,$64)" --play-and-exit
The command line input plays and exits properly, and plays to Line 1. The python code plays and exits, but not to Line 1.
Edit: I should mention I'm using python 3.4.4
Since shell is true the string representation of the list is passed to the command line which is not what you want. Look closely at the examples on this page https://docs.python.org/2/library/subprocess.html
This question already has answers here:
Correct way to write line to file?
(17 answers)
Closed 7 years ago.
How would I go about sending the output of a print command to a new file? I have a python script where I need to redirect the output at the end of the print statement to a file but I can't seem to find a way to accomplish the redirect. Why doesn't "print (stuff to be redirected) > newfile.txt" work?
Any help is appreciated!
As mentioned in this post, you could set the standard output to a file object.
import sys
sys.stdout = open('file', 'w')
Then, all your print statements should go directly to that file.
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 6 months ago.
So I have a script that needs to print to a file in a different directory. I give that absolute path and python doesn't like it.
Here's where the file is located:
C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt
(I know, long path, but QT plotter makes the file names really long)
I typed:
textfile = open('C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt', 'w')
And I get this error:
IOError: [Errno 22] invalid mode ('w') or filename:
I've read that I can use relative paths, but I am unsure how to give it a relative path with so many directories to go through.
Thanks!
The problem is that python is interpreting the backslashes in your path as escape sequences:
>>> 'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\x08uild-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
Notice that both \b and \n get translated to something else. Use a "raw" string instead:
>>> r'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\\numbers.txt'
I believe this answer here may be of help.
Essentially, your backslashes are causing issues.