urllib.urlretrieve creates file on disk - python-2.7

I do this:
thing = urllib.urlretrieve(url, "somefile.jpg")
It works, it gets the file, but it actually creates a file on the file system in the cwd. I write the file a little later to an appropriate path, but I don't want the file in the cwd at that time. What can I do?
Windows 10

If you want the file to go in a different directory, simply specify a path to where you want the file to go.
If you don't want the file to still be in the cwd after copying it, then delete the copy you don't want.
If you don't want to create a file at all, use something like urlopen.

There's a couple solutions 1. Change the cwd or 2. specify an alternate directory (as suggested by Scott Hunter), to do these:
1. Change your cwd (current working directory):
os.chdir("/Users/Desktop/SpecificDirectory/")
-now try your script.
OR
2. Specify an alternate directory in the download urllib command:
`thing = urllib.urlretrieve(url,"/Users/Desktop/SpecificDirectory/somefile.jpg")`

Related

use to import dta doesn't work for the file in folder?

I just did
use "data.dta"
My do file and dta are in the same folder. But it shows me error.
I also tried
use "datafolder/data.dta"
after placing data.dta into that folder. But this doesn't work either.
What should I write? It is not a good idea to write like
use "D://... /... /data.dta"
because other people should be able to run this code on his computer.
Before read the dta file, we need to tell Stata the current directory. Usually we can use cd command to change the current working directory. For example, if the data.dta is under the "C:\temp" folder, then use the following code:
cd "C:\temp"
use "data.dta"

read and write files to different directories Python

I want my code to go into a sub directory, perform some operation and save the output in a file which is one step up, to the main dir.
Main directory ---> sub_directory
I would appreciate solutions which do not require "hardcoding" the path of the main
directory. Is there a way I can directly write my file output to the main dir without
doing a os.chdir() every iteration? Something like just giving the path of the file to read and write?
For eg:
# example
import os
for i in xrange(10):
code to read and operate on some file in this sub dir one by one (ten files)
# write output file to the previous directory
# without hardcoding the path
code to write files to main directory (ten files )
You probably want to check the directory the file is operating within or check the current working directory:
import os
cur_dir= os.getcwd()
top_dir = os.path.dirname(cur_dir)
# perform operations in current directory
# do some stuff in top directory
Assuming you start in the main directory, and you know the (relative) path to the subdirectories, just do
open(os.path.join(subdir, filename))
to access a path in a subdirectory without actually changing the current directory.

Not able to create temp file in the desired directory

I need to create a temp file in a specified folder on MAC. However I am able to create the folder at the correct location but not able to create the file inside. I get an error saying it does not have permissions to create the file although i have passed 777 as file permission while creating the folder. please find my code below. I would like to mention that this problem is only on MAC. On windows it executes as expected. please let me know what the issue is.
wxString curDir = SeeWorkingDir() + wxT("Temp\\"); //Gets the working directory
if(!wxFileName::DirExists(curDir)) //Checks if directory exists
wxFileName::Mkdir( curDir, 0777, wxPATH_MKDIR_FULL ); // creates the directory
wxString jobcopy = wxFileName::CreateTempFileName(curDir); // should create temp file
I have absolutely no idea what your "SeeWorkingDir()" function is returning, but if it's returning the application directory then you shouldn't be writing anything into there.
Why not replace "SeeWorkingDir()" with a path to somewhere on the Macintosh that truly is writable, like the "/tmp/" directory?
You have not explained what your function SeeWorkingDir() does.
Maybe you should use wxFileName::GetTempDir to specify the directory where you store the file. This should be platform independent.
Or, in your call wxFileName::CreateTempFileName(curDir), you could specify the fileTemp parameter to open the file directly.
See the documentation:
wxWidgets 2.8.12 - wxFileName

How do I use fstream to write to a file above the executable's directory in C++?

I'm trying to write to a file that is not in the directory that the executable is in; I also want it to work no matter where the executable is (I believe that would rule out using ".."). I need this to work on Linux. Thank-you.
This has been asked already, see Get path of executable or Finding current executable's path without /proc/self/exe for a good answer, or search yourself.
Your problem boils down to getting the absolute path to the running executable.
A relative path is usually resolved starting from the running directory, which is not necessarily the executable directory (rather the current directory in the shell from which the executable is launched).
Under linux, you can read the directory of the executable with:
readlink /proc/self/exe
or you could use boost fs::path and fs::system_complete. Then you have to remove from that string the last component, which is the executable name.
Once you have the path of the executable directory, append "/.." to it and you will get the directory above the executable directory.
You can use an absolute path, if you know it ahead of time:
fstream * fs = new fstream("~/config_file");
If the file varies, you can take the path from user input or a configuration file.
Depending on where the file is, whether it moves and where the program is, you may actually be able to use a relative path. From the info you've given, I couldn't tell.
You can do this easily, but you will have to have an absolute path to the file you want to work on, or you will have to create some relative file-structure between your executable and the file you are wanting to access.
Another option is you could, using a forked process or popen(), launch find, and give it the appropriate arguments to locate the document you are wanting to work on, and then use that returned string as the argument to create the fstream object to write to or append to that file.
So for instance, this could look something like:
#include <limits.h>
#include <fstream>
#include <stdio.h>
char buffer[PATH_MAX];
//search the entire file-system starting from the root for "my_specific_file.txt"
FILE* located_file_handle = popen("find / -name my_specific_file.txt -print", "r");
//get the first file returned from the find operation and close the pipe
fgets(buffer, PATH_MAX, located_file_handle);
pclose(located_file_handle);
fstream file(buffer);
If you think there will be more than one file returned from the call to find, then you should cycle though each of them using fgets until you locate the one you want.

Relative path problem for a deployed win32 application

I have written a c++ program and deployed it in say c:\my_app, and my executable's path is c:\my_app\my_app.exe. Say, my_app needs many files such as the_file.txt, which is located in c:\my_app\the_file.txt.
In my executable, I open the txt file as, xx.open("the_file.txt");
Moreover, I have associated my program with let's say .myp extension.
When I'm on Desktop, and want to open a file named example.myp, my program can not see the_file.txt. Because, it (somehow) assumes that it's currently working on Desktop.
Is there any easy way to handle this problem by changing shell command for open in HKEY_CLASSES_ROOT? The naive solution would be to change all file open operations with something like %my_app_location/the_file.txt". I don't want to do that.
Always use a full path name to open a file. In other words, don't open "foo.txt", open "c:\bar\foo.txt". To find the install directory of your EXE use GetModuleFileName(), passing NULL for the module handle.
These days you shouldn't add files to c:\my_app....
Instead use the ProgramData Folder and full paths.
Use SHGetSpecialFolderPathA with CSIDL_COMMON_APPDATA to get the ProgramData folder and the create your program directory and add your files.
You should set current directory for your app's folder with SetCurrentDirectory function. After that you can open file by name without full path