I am trying to move PDF files from one folder to another using shutil.move. After I execute the script, python doesn't see them anymore, while they are still visible in Windows explorer. If I run the script again, python tells me there are no files. I have to run reverse script in order for python to see them. I use 64-bit Python v3.6.4 on Windows 10.
Command prompt with counts
import os, sys
import shutil
from os.path import join
Filez=0
PDFZ=0
filovi = os.listdir("C:/Users/Mirko1/pdfannots-master/NoGo")
for file in filovi:
Filez +=1
if file.endswith(".pdf"):
newdr=join('C:\\', 'Users','Mirko1','pdfannots-master','NoGo', file)
olddr=join('C:\\', 'Users','Mirko1','pdfannots-master', file)
PDFZ +=1
shutil.move(newdr, olddr)
print(Filez)
print(PDFZ)
Related
I created below simple program. It works if i run normally on sublime but when i create a exe using pyinstaller it does not work. It does not give any error it just shows program name and then terminates.
import os
from time import sleep
files = os.listdir('C:')
for file in files:
print(file)
sleep(5)
I'm trying to open Adv5KTCP.exe (path shown in code) but this exe actually opens three more avi.files as shown in image below. This causes the error. I've tried os, subprocess & pywinauto to call but to no avail. I've also tried adding the files path to environmental variable but I think that makes no sense. Opening the exe from command prompt does not work too. However, the exe can be opened manually by double clicking the exe file (like the usual). I just need it to be automated.
AVI Files:
The Error:
Double clicking the exe would open this window:
Here is my code:
import os
import sys, logging
import subprocess
import ctypes
from pywinauto import Application
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
exe = "Adv5KTCP.exe" #path set in environment variable: "C:\Program Files (x86)\Advantech\ADAM-5000TCP-6000 Utility\Program"
os.startfile(exe)
# subprocess.Popen([exe])
# application = Application(backend="uia").start(exe)
else:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
Does anybody have any idea on how I could go about this? Thank you in advance for your response.
New Findings:
I tried to cut the three AVI files to desktop & set desktop path to system variable & the exe gives the same error. However when i cut the exe file to desktop with the avi files as shown below, it works! Even when the other required files are not in desktop but path's set to system variable. Which means the exe is somehow registering the paths of the avi upon click, location/position or some sort which I'm not sure of.
I want a python line of code that sets the working directory to folder the code is part of. I am using spyder IDE for writing and running python codes.
Side note: This question is very similar to R command for setting working directory to source file location in Rstudio
This is a common problem I run into when developing in Jupyter for the command line.
You can try this to find where your script is executing from:
import os
from pathlib import Path
def myPath():
'''
return the current working directory in both interpeters and when exectued on the commandline
'''
try:
# path of this file when executed
wd = os.path.dirname(os.path.abspath(__file__))
except NameError as e:
print('this script is running in an interpreter')
# if not found
wd = Path().resolve()
return(wd)
I made a script that when clicked on it copies all the files of the directory where it was opened in on to a USB.
It works inside Pycharm but when I convert it to an executable (When I use pyinstaller to convert the .py to a .exec) it does not work.
I’m almost certain I know what’s wrong but I don’t know how to fix it.
import shutil
import os
current = os.getcwd()
list_of_files = os.listdir(current)
def get_files():
print('CURRENT: ' + current)
print('File_List: ' + str(list_of_files))
for files in list_of_files:
shutil.copy(current + '/' + files, '/Volumes/U/Copy_things')
get_files()
Long story short I’m using os.getcwd() so the file knows where it is located.
When I execute the file in Pycharm the current directory that os.getcwd() gives me is
CURRENT: /Users/MainFrame/Desktop/python_test_hub/move_file_test
But when I open the executable (same folder as the .py file ) and the terminal opens up os.getcwd() gives me is
CURRENT: /Users/MainFrame
So I need to find a way for the executable to open up the terminal where it is located so it can copy those files.
I want to be able to execute it from any folder and copy the files to a USB.
os.getcwd() gets the directory of where the script is executed from, and this isn't necessarily where you're script is located. Pycharm is most likely altering this path when executing the script, as it executes your script from the project path, rather than the python path.
Try os.path.abspath(os.path.dirname(os.sys.argv[0])) instead of os.getcwd().
These answers have more information: os.getcwd() vs os.path.abspath(os.path.dirname(__file__))
Difference between __file__ and sys.argv[0]
I would like to do a very simple thing but I am quite lost.
I am using a program called Blender and I want to write a script in python which open a .blend file but using the blender.app which is located in the same folder with the blend file, not with the blender.app which is located in Applications. (using Macosx)
So I was thinking that this should do the job...but instead it opens blender twice...
import os
path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open blender.app Import_mhx.blend")
I also tried this one
import os
path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open Import_mhx.blend")
but unfortunately it opens the .blend file with the default blender.app which is located in Applications...
any idea?
This cannot work since the system command gets executed in a subshell, and the chdir is only valid for that subshell. Replace the command by
os.system("open -a path/blender.app Import_mhx.blend")
or (much better)
subprocess.check_call(["open", "-a", os.path.join(path, "blender.app"),
"Import_mhx.blend"])
Have you tried telling the open command to open it WITH a specific application?
open -a /path/to/blender.app /path/to/Import_mhx.blend
Your first attempt was on the right track but you were really telling open to just open two different things. Not one with the other.