I want, when the user clicks on my executable file, the clicked file will be copied to a folder in C drive
I get the file name with its path by the following code:
Get_the_file_name = os.path.abspath(__file__)
And I intend to transfer the clicked file to the following address:
copy_path = 'C:/Users/joli/newfolder/copedfile.exe'
The entire program command
import shutil , os
try:
Get_the_file_name = os.path.abspath(__file__)
os.makedirs('C:/Users/ز/newfolder') #create Folder
copy_path = 'C:/Users/joli/newfolder/copedfile.exe'
shutil.copy(Get_the_file_name, copy_path)
except Exception as aa:
asass=open('C:/Users/rambod/Desktop/Vemv/eyefour/Error.txt' , 'w').write(str(aa))
No problem occurs when the above command is executed in the pycharm environment. However, after converting this command by Pyinstaller, the program encounters error 2.
Error text
[Errno 2] No such file or directory: 'C:\Users\joli\AppData\Local\Temp\_MEI88082\client.exe'
** ---> client.exe The name of the file has been clicked
import shutil , os
try:
Get_the_file_name = os.path.abspath(__file__)
os.makedirs('C:/Users/ز/newfolder') #create Folder
copy_path = 'C:/Users/joli/newfolder/copedfile.exe'
shutil.copy(Get_the_file_name, copy_path)
except Exception as aa:
asass=open('C:/Users/rambod/Desktop/Vemv/eyefour/Error.txt' , 'w').write(str(aa))
Related
I was making a project that has you type what's on the screen, and I was trying to get it to generate random words from a file. but I got an error saying FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'. I double-checked that the file was in the same directory and it was named right. Then I went back to my other program using this file, and it didn't work either (Even though it worked perfectly before). I tried using a shorter file to see if that was the problem, but it still gave me the same error.
Here is my (incomplete) code:
from Tkinter import Tk, Entry
import random
words = open('words.txt')
tab = Tk()
words = words.readlines()
totype = words[random.randint(1, len(words))]
print(totype)
How can I fix this?
Check that the directory you're running python from is the correct directory.
On python 3.4+, you can use the pathlib module to open files in the same directory as the script easily:
from pathlib import Path
p = Path(__file__).with_name('file.txt')
with p.open('r') as f:
words = f.read()
You can check this with:
import os
cwd = os.getcwd()
And use the relative directory from that.
Alternatively, use the full directory.
You can get the full path of the script using:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
Then you can use dir_path + file_name to get your full file path.
You have to give directory path specifying where the file is located if the file is located in same directory as your script file you can do
from Tkinter import Tk, Entry
import random
words = open('./words.txt')
tab = Tk()
words = words.readlines()
totype = words[random.randint(1, len(words))]
print(totype)
if it is located in some other folder then you can do
file = open(r'C:\Users\Directory\sample.txt') #example
words = open(file)
tab = Tk()
words = words.readlines()
totype = words[random.randint(1, len(words))]
print(totype)
Im trying to copy directorys and files from
/var/media/KODI/connman
to
/storage/.cache/connman
Inside the connman folder is a file and another folder with a file inside
The problem im having is that only the folder and its file are being copied the file in the connman folder is not. the code im working on is:
drive = xbmcaddon.Addon().getSetting('Drive')
connman = ('connman/')
src_network = ('/storage/.cache/')
for file_or_dir in os.listdir(drive + connman):
path = os.path.join(drive + connman,file_or_dir)
if os.path.isdir(path):
dir = file_or_dir
src = os.path.join(drive + connman,dir)
dst = os.path.join(src_network,dir)
try:
copytree(src,dst)
except Exception as e:
log(("copytree",e,src,dst))
Try this.
from distutils.dir_util import copy_tree
copy_tree("/source_folder", "/target_folder")
am learning python the hard way by zedd shaw and anytime i try to read files, it keeps giving me error that no such file or directory exist, even after the file exist a
from sys import argv
script, filename = argv, "testfile"
txt = open("testfile","r")
from sys import argv
script, filename = argv, "testfile"
txt = open("testfile","r")
IOError Traceback (most recent call
last) in ()
1 from sys import argv
2 script, filename = argv, "testfile"
----> 3 txt = open("testfile","r")
IOError: [Errno 2] No such file or directory: 'testfile'
If you are using windows you should configure your folder to show the known extensions in your filebrowser so you have the current naming.
here is a link to explain how to do it : https://www.thewindowsclub.com/show-file-extensions-in-windows
if the naming of your file was good you can use absolute path safely with :
import os
current_dir = os.path.dirname(__file__) # Absolute directory of the current python file
filename = "testfile.txt"
abs_file_path = os.path.join(current_dir, filename)
I have not used this way to access files, but in general, doing it that way you need to have the file in the folder your script is in and you need a file extension like .txt or .docx, you do not have an extension so it is looking for a file, not a text file.
import sys
import os
import re
import ftplib
os.system('dir /S "D:\LifeFrame\*.jpg" > "D:\Python\placestogo.txt"') #this is where to search.
dat = open('placestogo.txt','r').read()
drives = re.findall(r'.\:\\.+.+',dat)
for i in range(len(drives)):
path = drives[i]
os.system('dir '+ path +'\*.jpg > D:\python\picplace.txt')
picplace = open('picplace.txt','r').read()
pics = re.findall(r'\w+_\w+.\w+..jpg|IMG.+|\w+.jpg',picplace)
for i in range(len(pics)):
filename = pics[i]
ftp = ftplib.FTP("localhost")
print ftp.login("xxxxxxxx","xxxxxxxx")
ftp.cwd("/folder")
myfile = open(path,"rb")
print ftp.storlines('STOR ' + filename, myfile)
print ftp.quit()
sys.exit()
i am trying to copy all of those files to my ftp server but it gives me that error:
d:\Python>stealerupload.py
230 Logged on
Traceback (most recent call last):
File "D:\Python\stealerupload.py", line 22, in <module>
myfile = open(path,"rb")
IOError: [Errno 22] invalid mode ('rb') or filename: '"D:\\LifeFrame"'
any one knows where is the problem ? I am running as administrator and folder should have permissions
The error seems pretty obvious. You are trying to open a directory path, which is neither possible nor what you actually want to do. This bit:
for i in range(len(drives)):
path = drives[i]
...
for i in range(len(pics)):
...
myfile = open(path,"rb")
Inside the loop you are setting path to be one of your drives elements. each of these items appears to be a directory path. Then you try to open path later, which is a directory path and not a file.
In the error message, it shows '"D:\\LifeFrame"', which to me looks like you have extra quotes in path. Try adding print path to see what its value is.
Maybe you want to upload data from filename to your server, not from path, in which case Python showed in the error message is where the bug is: you should be opening filename instead.
In Python, I'm trying to open a file that gets saved to the %TEMP% directory. I've tried:
file = open("%TEMP%\file.txt")
and
file = open("%%TEMP%%\file.txt")
and
file = open("%TEMP%\\file.txt")
and
file = open("%%TEMP%%\\file.txt")
And always get (this one specifically for that last example):
IOError: [Errno 2] No such file or directory: '%%TEMP%%\\file.txt'
For sanity's sake, from Windows command prompt I do a type %TEMP%\file.txt and it prints out the file OK. Any help?
Use os.environ
import os
f = open(os.path.join(os.environ['TEMP'], 'file.txt'))
You can also use os.path.expandvars
import os
f = open(os.path.expandvars(r'%TEMP%\file.txt'))