I get the wrong path back. The Datafile is in D:... and get everytime the path C:\Python27\lib\site-packages\xy back from python. I use the function
path = getcwd()
How can I fix it?
You may be executing the script in a different place than your intended directory.
Solution 1: Move the .py file to the target directory, and execute it there.
Pros:
Easy
Works cross-platform (and for other users - if you do this, use getcwdu for Unicode)
No hard-coded path strings
Cons:
File must be in the same or higher directory as target folder
Solution 2: Manually write the string of the path to the folder.
Pros:
'Just Works'
Cons:
Annoying bugs w/typos
Need to re-code every time you change directories
Won't work anywhere else
In my idea, make sure your IDE has your target directory opened as your project folder.
After all, it's a debug-time error, and won't affect the smooth running of your program in the runtime, if your program's launch script has the right address for your program to run, and does its part normally!
By the way #order, I'm totally against hardcoding a path into your code, because it's just a very lame programming exercise!
Just get yourselves used to going through the right exercise, although it doesn't seem very beneficial in the short run. in the coming years of your career, you're going to be against using the cons and pros of such a comparison, but it will become a right or wrong coding exercise!
Cheers.
Related
I am creating a one file executable and curious about the runtime-tmpdir parameter. As the one file always creates temp files in OS temp dir whenever opened, I want to change that with this option. But I am a bit worried before using this parameter as in the docs of Pyinstaller it is mentioned- "Please use this option only if you know what you are doing".
Its a bit silly question but will it affect the performance or any other system files?
Personally, the mention didn't bother me. It should not affect performance, but it is possible for errors, depending on how your code is structured. I use "--runtime-tmpdir" and I'm fine :)
I've been working on a project lately which requires me to get a value stored in a text file. Simple task, right?
I have tried pretty much every stupid solution that uses readline() at some point, but when it's printed, there's nothing. Debug also tells me that it is empty.
Since I've experienced some inconsistencies with python already, I tried using the same function inside a different python file, and running it does exactly what it's supposed to do.
My current solution (not the best, but currently I just want it to run properly) is
count_file = "count.txt"
filehandle = open(count_file, 'r')
line = filehandle.readline()
print(line)
which works fine when used in a file called "test.py". When used in my "main.py", it returns nothing. And yes, I used it on the same context and temporarily deleted everything else from the main file while testing.
Does anyone have a clue what causes this? I could just paste the other stuff into the test file and rename it, but 1. that's annoying and 2. it'd be useful to know how to avoid this.
EDIT: I am not certain yet, but I think that the problem is caused by my IDE (am using the latest version of Pycharm Professional). I solved the problem by deleting and re-adding the Run/Debug Configuration.
Sometimes you will get return value as None if you do not specify the file path. please check the path/relative path of "count.txt" file with respect to "main.py" script.
First point: your file path is relative, so it will be resolved according to the current working directory, NOT depending on your script's location.
Second point: you read a single line, it can indeed be empty or only contains non-printable characters.
Third point: run your code without pycharm. Pycharm is probably fine when you really understand what it does behind your back, but if you don't you will indeed experiment some unexpected results. Just using the command line and a simple code editor is actually much simpler (though possibly not as productive) and less confusing.
And finally, don't forget you have an interactive Python shell. Quite handy for testing out things...
I've been given an educational assignment during which I write Gherkin scenarios to test a website using Python 3.6, Splinter and Behave. I'm making some pretty good progress but I'm stuck on this little thing. Currently I've succeeded in getting a file to download through a headless instance of Chrome in Ubuntu. However, for the last step of the scenario to pass, I need to verify the file's existence. After a lot of searching I've found a method that works, which is:
assert os.path.isfile('/home/[USERNAME]/Downloads/file.csv')
However, in order to make this test more compatible with other computers, I'd like the path to the file to be shorter and simpler. Most importantly, not using this system's username.
I'm new to all of this so this could very well be a dumb question, but I've been searching all over the place and I simply can't find an answer.
You could rewrite the path using the ~ which replaces /home/[USERNAME]/, so it would become ~/Downloads/file.csv. Then, you could use Python's os.path.expanduser() function as follows:
assert os.path.isfile(os.path.expanduser('~/Downloads/file.csv'))
os.path.expanderuser() will automatically expand it to /home/[USERNAME]/ for you.
If you need to get Downloads folder path, than you can use answer to this question: python - Finding the user's "Downloads" folder
So I finished writing a Python program that handles some sample banking info.
I was told to create a makefile for the assignment that will have build,view and clean targets. I've gotten that much done and it works as anticipated.
However, I was told that my instructor would run the program similar to
accounts -i
where accounts is the program and -i is the arg.
However, I have only been able to use
./accounts -i
to run the program.
I looked around and I found something about adding the file to PATH but I am really lost as to what I am doing. Is there something wrong on my end or is my instructor telling me to do something wrong?
Thanks.
You can add . (the current working directory) to your path with something like (bash/ksh syntax) :
export PATH="$PATH:."
...but this is widely regarded as a security problem.
You're probably best off just typing ./accounts -i. Your instructor will probably already know the issue.
Personally, I would imagine that this was just a simple typo or something like it by your instructor.
In shell scripting, the PATH variables denotes a set of directories (separated by colons) that will be searched for the executable that you are referencing.
So assuming your PATH is equal to ".:/usr/bin", when you type "accounts -i" at the command line, your shell with first search for "./accounts" and then (assuming it does not exist) check for "/usr/bin/accounts". If it finds either it will then execute the one that exists and pass the given arguments (i.e. "-i").
If you would like to try this out, PATH is generally modified like this:
export PATH="directory_name:$PATH"
(as it is most likely that you will be wanting to have "directory_name" take precedence over the rest of what is current in $PATH).
However, "." is generally not included in PATH by default, which is considered to be bad practice by many for reasons detailed here.
So my guess is your instructor either has "." in his or her PATH, or will quickly realize that using "./acccounts" is more appropriate.
I'm writing a script that emulates a unix environment with python (yes, I know that may sound silly).
Basically I set up the "pwd" and "ls" commands before the "cd" command. Now I need my python script to navigate itself around directories by moving itself. I was wondering if anyone could point me in the right direction? eg.What commands to use, etc
Note. I would prefer it if I only had to use built-in modules.
Thank You!
I am not sure I understand you properly, but if you are talking about moving through the standard unix file system and not copying the script to different locations, you could easily just wrap the three commands (pwd,ls,cd) as python functions, just as you were describing.
In fact, I did this as part of the wx_py project; they aren't perfect clones, but they work well enough. Here are the three functions:
import os
import glob
def pwd():
print os.getcwd()
def cd(path,usePrint=True):
os.chdir(os.path.expandvars(os.path.expanduser(path)))
if usePrint:
pwd()
def ls(str='*',fullpath=False):
g=glob.glob(os.path.expandvars(os.path.expanduser(str)))
if fullpath:
for i in g:
print i
else:
for i in g:
print os.path.split(i)[1]
The full file is at:
https://github.com/davidmashburn/wx_py/blob/master/wx_py/path.py
I'm sure others may have done a better job, so please just take these functions as a starting point.