Best Practice Question

  • Thread starter Anthony Correia
  • Start date
A

Anthony Correia

Just started learning Python. I just wrote a simple copy files script. I use Powershell now as my main scripting language but I wanted to extend into the linux platform as well. Is this the best way to do it?

import os

objdir = ("C:\\temp2")
colDir = os.listdir(objdir)
for f in colDir:
activefile = os.path.join(objdir + "\\" + f)
print ("Removing " + activefile + " from " + objdir)
os.remove(activefile)

In Powershell I would do this:

$colDir = gci -path "c:\temp2"
$objDir = "C:\temp3"
ForEach($file in $colDir){
#.Fullname lists the directory and filename together. No need to do a join
#beforehand.
Copy-item $file.fullname -destination $objDir
}
 
D

Dave Angel

Just started learning Python. I just wrote a simple copy files script. I use Powershell now as my main scripting language but I wanted to extend into the linux platform as well. Is this the best way to do it?

import os

objdir = ("C:\\temp2")
colDir = os.listdir(objdir)
for f in colDir:
activefile = os.path.join(objdir + "\\" + f)
print ("Removing " + activefile + " from " + objdir)
os.remove(activefile)

In Powershell I would do this:

$colDir = gci -path "c:\temp2"
$objDir = "C:\temp3"
ForEach($file in $colDir){
#.Fullname lists the directory and filename together. No need to do a join
#beforehand.
Copy-item $file.fullname -destination $objDir
}

You started two nearly-identical threads, with nearly the same content.
I won't repeat the comments already posted in the other thread, but
notice that your powershell script copies the file, while your Python
"translation" deletes the file. Big difference.

Next, you should use raw strings, or at least use the forward slash,
rather than double backslashes in file path literals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top