I'm trying to figure out how to duplicate a single file for example: I have a file in "C:\ ..." and would like to duplicate this exact same file once. If also possible is there a way to use python to open specific documents?
thanks
I think you might be looking for copy2. This will copy the file contents and as much of the file metadata (permissions, ownership, etc) as it can on the platform. copystat has more notes on what can and can't be copied and how to find out on your platform.
just using system() from os module
os.system("cp resource_file target_file")
Using a module named shutil, the function copy2 can be called with the path of the source file and the corresponding destination directory you want to write to. For example,
import shutil
shutil.copy2('/src/test.txt','/dst/test_copy.txt')
you can copy a file via the command line on windows: open cmd.exe then type cd "C:\ ..." then type copy yourfile destination. more info here
you can make python do this for you: you will need the subprocess module which comes integrated into python so you dont have to donwload anything.
like this:
import subprocess
subprocess.run('copy yourfile destionation')
remember that for this to work your python script should be in the same folder as "yourfile", why? because i type "yourfile" as a relative path relative vs absolute path
subprocess works for python 3.3 and newer versions so another way to do it would be:
import os
os.system('copy youfile destionation')
to open specific documents with python take a quick look at here: https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
Related
My question is I am unable to run a python file in VS Code terminal unless I specify the full path.
Whereas, when I see any python tutorial, only python filename is entered and it works.
Can somebody pls help me with this issue?
The files need to be in the same folder where you are operating / or have the other Python files.
You can use two ways for this:
first:
import os
pwd = os.getcwd()
# again, make sure you file is in the same directory.
the_file = (pwd + "\\filename.xlsx")
or Secondly,
#the below you can use wherever your file is and it will locate it.
# you specify the full path using pathlib.path and:
pathlib.path(directory here without the file itself).joinpath(file name here).
Hi I am having a really frustrating time trying to import a json file to my python script.
When I run the file individually it works fine. However I am trying to import the file into another area of my system
import sys
sys.path.append(r'/home/seamus/code/544-dss/controllers/google')
import google_worksheets
test = google_worksheets.g_sheets()
print(test.service)
When I do this it works until hitting this line of code in my class
ServiceAccountCredentials.from_json_keyfile_name('newcred.json',
self.scope)
Then I get the error message:
No such file or directory: 'newcred.json'
The file newcred.json is in the same directory as google_worksheets and when I run google_worksheets on it's own it works fine (right click file and click run).
Any suggestion on how to fix this would be greatly appreciated as this is very annoying?
My folder structure is like this:
/home/seamus/code/544-dss/controllers/google
with init.py, google_worksheets.py and newcred.json all living inside the google folder.
Thanks very much
Relative path are resolved againts the current working directory, not against the path of the python script or module trying to access the file. You have to use an absolute path. You can use the __file__ magic variable and os.path function to build this absolute path from your module.
So I have an .exe tool that needs to be executed in cmd that's used to convert some GIS data from one format to BAG format. The syntax looks like so:
C:\Caris\BDB\4.0\bin > carisbatch -r SurfacetoBAG [options] "input_file" "output_file"
I have a directory of about 40 files of GIS data, so I'd like to know how to automate the above cmd tool such that it will run through use all 40 files as "input_file". I've looked into using the subprocess() module wrapped in a for loop with python, but I am under the impression it can only be used for unix systems. Any ideas?
If you import os, you should be able to use
os.system('your command')
Regardless of platform (of course, the command string will vary between platforms)
you can use subprocess no problem on windows ...
example to follow
import subprocess
for file in my_files:
subprocess.Popen(["C:/Caris/BDB/4.0/bin/carisbatch.exe",'-r','SurfacetoBAGfile',file+".output"])
if you need to do it without the list format
for file in my_files:
subprocess.Popen('C:/Caris/BDB/4.0/bin/carisbatch.exe -r SurfacetoBAGfile "{0}" "{0}.output"'.format(file),shell=True)
This is my code. I'm pretty new to this.
from subprocess import call
call(["cd", "/etc/apache2/"])
However, when this function is run, I get
Errno 2: No such file or directory
I am running Django within Apache*. This is my views.py file. Ask for additional code, and you shall receive.
edit - It should be noted that /etc/apache2/ does indeed exist.
If you want to change the working directory of the Python process you can use chdir from the os module:
import os
os.chdir('/etc/apache2')
First of all, you will not get what you expect if you run this. Try
import os
os.chdir('/etc/apache2')
Second, try /path/to/cd as process may not know cd alias.
I am wanting to use portable python 2.7.x to connect to an Access database. I can't seem to get it working as it doesn't have the pyodbc libraries. Is there another way to use portable python to connect?
The newest version of portable python has an option to install pyodbc but you have to select the option it doesn't go in by default.
Click on the modules option
Select the option for pyodbc
I have did it in different way.. .
follow what i have just done on my mac snow leopard!!
Download the the pyodbc's source from where it is on internet.
Extract and 'cd' into that dir.. . Run 'python setup.py build' and then take 'pyodbc.so' file from that build's dir. Make new python file named as 'pyodbc.py' and write the content given below.(and put that 'pyodbc.so' file with it)
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__,'pyodbc.so')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
(remember put above code in file named as 'pyodbc.py' and put that 'pyodbc.so' file with that)
and at last ..put all these where ever you want to use or in run time add that location into sys.path as:
>>> import sys
>>> sys.path.insert(0,"/my_portable/location") # location to dir which contains those two files
after doing all this i have put those two files with my test python file..and in that i am able to import 'pyodbc' without installing it.
>>> import pyodbc
>>> dir(pyodbc)